Register ExampleΒΆ

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <cl.h>

#define VERSION "0.1.0"

#define REGION "na"
#define ASSOCIATION_TOKEN "4c268ad585630e9f"

void help(const char *app_name);
void registration_handler(const unsigned char *str_json, size_t length);
void registration();

int main(int argc, char **argv)
{
    int c;
    
    while ((c = getopt (argc, argv, "vh?")) != -1)
    {
        switch(c)
        {
            case 'v':
                printf("%s", VERSION);
                exit(1);
                break;
                    
            case 'h':
            case '?':
            default:
                help(argv[0]);
        }
    }

    registration();
    
    return 0;
}

// ******************************************

void help(const char *app_name)
{
    printf("%s v%s.\n\n", __FILE__, VERSION);

    printf("Register this device to the server.\n\n");
    
    printf("Usage: %s [-h|-?] [-v]\n\n", __FILE__);
    
    printf("   -h          This help\n");
    printf("   -v          Get the version\n\n");
    
    exit(1);
}

/*
{"id":"[DEVICE_ID]","plantId":"[PLANT_ID]","mqtt":{"mqttUsername":"[MQTT_USERNAME]","mqttPassword":"[MQTT_PASSWORD]"}}
*/
void registration_handler(const unsigned char *str_json, size_t length)
{
    printf("json: %s\n", str_json);
}

void registration()
{
    char json[512];
    cl_return_t result;

    printf("[.]. Initialize context...\n");
    strcpy(cl_handle.developer_key, DEVELOPER_KEY);
    strcpy(cl_handle.iot_brand_name, IOT_BRAND_NAME);
    strcpy(cl_handle.iot_model_name, IOT_MODEL_NAME);
    strcpy(cl_handle.iot_submodel_name, IOT_SUBMODEL_NAME);
    strcpy(cl_handle.iot_system, IOT_SYSTEM);

    // INIT
    printf("[.]. Initialize...\n");
    result = cl_register_init(&cl_handle);
    switch (result) {
        case CL_SUCCESS:
            printf("[+]. Initialization ok.\n");
            break;
        
        case CL_FAILURE: 
        default:
            // OTHER
            printf("[-]. Failed to init the library register.\n");
            break;
    }
    
    printf("\n");
    
    printf("[.]. Set the region...\n");
    cl_register_set_region(REGION);
    
    /*
    {
        "mac": "%s",
        "brand": "%s",
        "submodel": "%s",
        "version": "%s",
        "ip": "%s",
        "associationToken": "%s"
    }
    */
    // REGISTRATION
    printf("[.]. Registration...\n");
    memset(json, 0, sizeof(json));
    sprintf(json, "{ \
                    \"mac\":\"%s\", \
                    \"brand\":\"%s\", \
                    \"submodel\":\"%s\", \
                    \"version\":\"%s\", \
                    \"ip\":\"%s\", \
                    \"associationToken\":\"%s\" \
                    }",
                    "Mac_Ingenic_#01",
                    IOT_BRAND_NAME,
                    IOT_SUBMODEL_NAME,
                    VERSION,
                    "192.168.1.10",
                    ASSOCIATION_TOKEN
                    );
    result = cl_register_registration(json, registration_handler);
    switch (result) {
        case CL_SUCCESS:
            printf("[+]. Registered successfully on backend.\n");
            break;
        
        case CL_CONNECTION_FAILED:
            printf("[-]. CL_CONNECTION_FAILED.\n");
            break;

        case CL_DEVICE_NOT_FOUND:
            printf("[-]. CL_DEVICE_NOT_FOUND.\n");
            break;

        case CL_FAILURE: 
        default:
            // OTHER
            printf("[-]. Unable to register on backend.\n");
            break;
    }
       
    printf("\n");
    
    // DEINIT
    printf("[.]. Deinit...\n");
    result = cl_register_deinit();
    switch (result) {
        case CL_SUCCESS:
            printf("[+]. Deinit ok.\n");
            break;
        
        case CL_FAILURE: 
        default:
            // OTHER
            printf("[-]. Failed to deinit the library register.\n");
            break;
    }
}