Light C API Example

Exemple simplifié API C pour systèmes embarqués

weight: 380 linkTitle: “C Light Example”

 

1. Librairie (libs)

This library contains all the headers of the library, as well as the static archive lib_KalimaMQC.a. This archive will be used when creating the executable of the project. The headers will be used to call the methods that will be useful to us in our example.

 

2. Config file (etc/cfg)

Picture 1

Let’s see the usefulness of the different configurations:

  • LedgerName -> This is the name of the Ledger we are going to connect to. For the example it is not very useful.

  • NODE_NAME -> This is the name of the node we create. It is also not very useful for our example.

  • Privachain -> This is the privachain we will be connected into. If you want to connect to another Kalima Blockchain, you will have to change the privachain.

  • FILES_PATH -> This is the directory in which all Kalima related files will be created. When you launch your node for the first time, this directory will be created if it doesn’t already exist, and you will find inside the files for RSA, DevID and logs.

  • BLOCKCHAIN_PUBLIC_KEY -> This is the path for the blockchain public key if you need for some reasons to have it locally. It isn’t used for now.

  • SerialID -> SerialId  This ID allow your node to be authorized on the Kalima Blockchain which you can obtain here: https://inscription.tuto.kalimadb.com/airdrop (you have 10 serialId received by email)

 

 

3. Makefile

Picture 10

The makefile will allow us to create the executable to run the project. First it will create the objects from source file main. Finally, it will create the executable main.run from the object created before and the archive “lib_KalimaMQCa” located in the lib directory.

To execute the makefile, you just must type “make” in your terminal (you have to be in the project path in your terminal).

To clear the objects, the executable and the logs, you just need to type “make clean”.

4. Main project (src + inc)

Picture 11

 

This is the start of the main function. It contains the Kalima Node initialization functions.

Here is the explanation on the useful Kalima function:

  • create_Node : Will create a Kalima Node with information from the config file (by default “etc/cfg/config.txt”, you can also use a different one by putting it as a parameter when launching the executable). The node is the main component which will allow communication with the Blockchain. The NULL parameter is because that’s where you put the smart contract list when you are using smart contracts. To see how it works, use the C smart contract example.
  • Connect_to_Notaries : Will start the connection to the Blockchain with the Node and Callback as a parameter.

When creating the node, we will also create a random deviceID that will be encrypted and written in the “DeviceID” directory that will be created in the location where you start the executable from. If this encrypted file already exists, it will not be recreated. The node will just decrypt the file and use the decrypted deviceID. This deviceID, along with the SerialID in your config file, will allow the Blockchain to identify our node and allow us to send data. An RSA directory will also be created containing a public key and a private key used to encrypt communications with the blockchain.

This example when launched will offer the user two options:

  • Sending a default message

Picture 12

 

This choice sends the message put by the user to the Blockchain  address “/ sensors” with the key “Default message”. The time to live (ttl) is put as “-1” which means that message will stay in the blockchain memcache.

  • Fully configurable message

Picture 13

Here we build the message from scratch.

  1. First,  the user is offered choose between adding or deleting a message. Then we retrieve the adress, the key and the message from the user’s terminal. Finally, we send the message. Unlike the previous example, the message will remain here indefinitely.

 

5. Running the project

1. Authorization

Before launching the project, it is necessary that you do the correct procedure to be authorized on the Blockchain.

For this, it is fairly simple, you need to contact us and send the serialID you put in your config file. Once you are authorized, you have 5 minutes to launch the project to be fully authorized. Once it is done your node should be properly launched and the directoried “RSA” and “DevID” should have been created.

The next time you launch the project, as long as the directories created stay the same and the serialID is also the same, everything should still work.

If for some reason your directories got deleted, you changed your system or you changed your serialID, the authorization process must be done again.

 

2. Launch

To run the example, simply open a terminal and move to the main folder (where your makefile is).

Then simply type “make” to launch the makefile seen above. This command will create the executable file “main.run” (as well as the DevID directory and the RSA directory) that will allow us to launch the program.

Finally, you must write “./main.run” to launch the program.

When you reach this point, and you are sure you did all the correct steps to be authorized by the Blockchain, it means your Node has been successfully launched.

It also means that you received all the memcache data of the Blockchain and the Lua smart contract has been downloaded to the directory you specified in the main file (the contract directory will be created if it does not already exist).

You are now free to send data to the Blockchain using either of the options seen previously. If you receive a data with the criteria of your smart contracts, they will be loaded and used.

 

6. Possible issues

If your message do not appear in the blockchain, here are some possible errors:

  • Verify that the SerialID in the config file is the same as the one entered in the blockchain.

  • If you delete a DeviceID or RSA directory, the new calculated files will be different. It will therefore be necessary to redo the authorization process.

  • If you have made changes on the hand that are not considered, make a “make clean” before redoing “make” so that all the changes are considered.

  • If you have other problems, you can look at the logs when the program has problems and contact us.