Example #1
0
kaa_error_t kaa_init_security_stuff(void)
{
    sndc_file_ref_t key_file = sndc_file_open(KAA_KEY_STORAGE, DE_FRDONLY);

    if (key_file) {
        sndc_file_seek(key_file, 0, SEEK_END);
        kaa_public_key_length = sndc_file_tell(key_file);
        kaa_public_key = (char*)sndc_mem_calloc(kaa_public_key_length, sizeof(char));

        if (kaa_public_key == NULL) {
            sndc_printf("Failed to allocate %u bytes for public key\n", kaa_public_key_length);
            return KAA_ERR_NOMEM;
        }

        sndc_file_seek(key_file, 0, SEEK_SET);
        if (sndc_file_read(key_file, kaa_public_key, kaa_public_key_length) == 0) {
            sndc_mem_free(kaa_public_key);
            sndc_printf("Failed to read public key (size %u)\n", kaa_public_key_length);
            return KAA_ERR_INVALID_PUB_KEY;
        }
        sndc_file_close(key_file);
        sndc_printf("Restored public key (size %u)\n", kaa_public_key_length);
    } else {
        sndc_printf("No RSA key file %s found\n", KAA_KEY_STORAGE);
    }

    ext_calculate_sha_hash(kaa_public_key, kaa_public_key_length, kaa_public_key_hash);
    sndc_printf("SHA calculated\n");
    return KAA_ERR_NONE;
}
Example #2
0
kaa_error_t kaa_on_configuration_updated(void *context, const kaa_root_configuration_t *configuration)
{
    int i = 0;
    sndc_printf("Configuration updated\n");
    kaa_list_node_t *it = kaa_list_begin(configuration->light_zones);
    while (it) {
        kaa_configuration_light_zone_t *zone = (kaa_configuration_light_zone_t *) kaa_list_get_data(it);
        if (zone->zone_id >= 0 && zone->zone_id < LIGHT_ZONES_COUNT) {
            switch (zone->zone_status) {
            case ENUM_ZONE_STATUS_ENABLE:
                if (light_states[i] != LIGHT_ON) {
                    sndc_io_write(light_zones[zone->zone_id], LIGHT_ON);
                    light_states[i] = LIGHT_ON;
                }
                break;
            case ENUM_ZONE_STATUS_DISABLE:
                if (light_states[i] != LIGHT_OFF) {
                    sndc_io_write(light_zones[zone->zone_id], LIGHT_OFF);
                    light_states[i] = LIGHT_OFF;
                }
                break;
            }
        }
        it = kaa_list_next(it);
        i++;
    }
    for(; i < LIGHT_ZONES_COUNT; ++i) {
        if (light_states[i] != LIGHT_OFF) {
             sndc_io_write(light_zones[i], LIGHT_OFF);
             light_states[i] = LIGHT_OFF;
        }
    }

    return KAA_ERR_NONE;
}
Example #3
0
void ext_write_log(FILE * sink, const char * buffer, size_t message_size)
{
    if (!buffer) {
        return;
    }
    sndc_printf(buffer);
    sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);
}
Example #4
0
static void start_client(void)
{
   sndc_printf("Connecting as client...\n");

   /* Clear all profiles */
   sndc_profile_eraseAll();

   /* clean the local profile and read the stored valies */
   sndc_profile_t *connect_profile = sndc_profile_getNew();

   /* lock the profile for direct writing */
   sndc_profile_writeLock();

   /*copy ssid*/
   sndc_profile_set_ssidStr(connect_profile, SSID_STR);

#ifdef USE_ENCRYPTION
   sndc_profile_set_passphraseStr(connect_profile, PASSPHRASE);
   sndc_profile_set_wpa1wpa2(connect_profile, TRUE);
#else
   sndc_profile_set_open(connect_profile, TRUE);
#endif

   /* work as client station*/
   sndc_profile_set_role(connect_profile, SNDC_PROFILE_ROLE_CLIENT);

   /* do not keep profile */
   sndc_profile_set_deleteOnDisconnect(connect_profile, TRUE);

   /* unlock the profile */
   sndc_profile_writeUnlock();

   /* request connection to the specific profile */
   sndc_cm_profileConnect(connect_profile);

   /* Release profile reference */
   sndc_profile_release(connect_profile);
}
Example #5
0
void thread_run_fn(uintptr_t arg)
{
    if(!arg) {
        sndc_printf("Kaa client thread function Error, no args\n");
        return;
    }

    kaa_client_t *self = (kaa_client_t *)arg;
    KAA_LOG_TRACE(self->kaa_context->logger, KAA_ERR_NONE, "Kaa client working thread started....");
    sndc_sem_post(&self->logging_semophore);

    sndc_sock_fdset r_set;
    sndc_sock_fdset w_set;
    sndc_sock_fdset x_set;
    int r = 0;
    int max_fd = 0;
    uint32_t msec = 0;
    int ops_fd = -1, bootstrap_fd = -1;
    bool_t fdset = false;
    uint16_t timeout = self->max_update_time;
    kaa_error_t error_code;
    KAA_LOG_TRACE(self->kaa_context->logger, KAA_ERR_NONE, "Kaa client working thread(%s) wait starting...", self->thread_name);
    sndc_sem_wait(&self->start_semophore);
    KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Kaa client working thread(%s) started", self->thread_name);

    while(self->operate) {
        max_fd = 0;
        SNDC_FD_ZERO(&r_set);
        SNDC_FD_ZERO(&w_set);
        SNDC_FD_ZERO(&x_set);

        // This semaphore is used to synchronize main thread and kaa_client thread,
        // mostly for logging proposes.
        sndc_sem_tryWait(&self->logging_semophore);

        if (self->operations_channel.context)
            kaa_tcp_channel_get_descriptor(&self->operations_channel, &ops_fd);
        if(self->bootstrap_channel.context)
            kaa_tcp_channel_get_descriptor(&self->bootstrap_channel, &bootstrap_fd);
        KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: descriptors: bootstrap(%d), operations(%d)", bootstrap_fd, ops_fd);

        print_mem_stat(self);

        if (bootstrap_fd >= 0) {
            fdset = false;
            if (kaa_tcp_channel_is_ready(&self->bootstrap_channel, FD_READ)) {
                SNDC_FD_SET(bootstrap_fd, &r_set);
                KAA_LOG_DEBUG(self->kaa_context->logger,
                        KAA_ERR_NONE,
                        "IO LOOP: Bootstrap READ set wait");
                fdset = true;
            }
            if (kaa_tcp_channel_is_ready(&self->bootstrap_channel, FD_WRITE)) {
                SNDC_FD_SET(bootstrap_fd, &w_set);
                KAA_LOG_DEBUG(self->kaa_context->logger,
                        KAA_ERR_NONE,
                        "IO LOOP: Bootstrap WRITE set wait");
                fdset = true;
            }
            if (fdset) {
                max_fd = MAX(max_fd, bootstrap_fd);
                SNDC_FD_SET(bootstrap_fd, &x_set);
            }
        }
        if (ops_fd >= 0) {
            fdset = false;
            if (kaa_tcp_channel_is_ready(&self->operations_channel, FD_READ)) {
                SNDC_FD_SET(ops_fd, &r_set);
                KAA_LOG_DEBUG(self->kaa_context->logger,
                        KAA_ERR_NONE,
                        "IO LOOP: Operations READ set wait");
                fdset = true;
            }
            if (kaa_tcp_channel_is_ready(&self->operations_channel, FD_WRITE)) {
                SNDC_FD_SET(ops_fd, &w_set);
                KAA_LOG_DEBUG(self->kaa_context->logger,
                        KAA_ERR_NONE,
                        "IO LOOP: Operations WRITE set wait");
                fdset = true;
            }
            if (fdset) {
                max_fd = MAX(max_fd, ops_fd);
                SNDC_FD_SET(ops_fd, &x_set);
            }
        }

        kaa_tcp_channel_get_max_timeout(&self->operations_channel, &timeout);
        self->timeval.tv_sec = timeout;
        if (timeout > self->max_update_time)
            self->timeval.tv_sec = self->max_update_time;

        self->timeval.tv_usec = 0;
        sndc_sem_post(&self->logging_semophore);
        r = sndc_sock_select(max_fd+1,&r_set,&w_set,&x_set,&self->timeval);
        sndc_sem_tryWait(&self->logging_semophore);
        if (r == 0) {
            msec = sndc_sys_getTimestamp_msec();
            KAA_LOG_DEBUG(self->kaa_context->logger,
                    KAA_ERR_NONE,
                    "IO LOOP: timeout (%d) expired",
                    self->timeval.tv_sec);

            if (self->bootstrap_state == BOOTSRAP_FINISHED && bootstrap_fd == -1) {
                sndc_printf("Bootstrap channel deinit, Operations channel init %d\n", bootstrap_fd);
                KAA_LOG_INFO(self->kaa_context->logger,
                                    KAA_ERR_NONE,
                                    "IO LOOP: Bootstrap channel finish processing switching to Operations channel");
                kaa_client_deinit_bootstrap_channel(self);
                kaa_client_init_operations_channel(self);
            }

            if (self->bootstrap_channel.context) {
                error_code = kaa_tcp_channel_check_keepalive(&self->bootstrap_channel);
                if (error_code) {
                    KAA_LOG_ERROR(self->kaa_context->logger,
                            KAA_ERR_NONE,
                            "IO LOOP: Failed Keepalive Bootstrap(%d) check",
                            bootstrap_fd);
                }
            }
            if (self->operations_channel.context) {
                error_code = kaa_tcp_channel_check_keepalive(&self->operations_channel);
                if (error_code) {
                    KAA_LOG_ERROR(self->kaa_context->logger,
                            KAA_ERR_NONE,
                            "IO LOOP: Failed Keepalive Operations(%d) check",
                            ops_fd);
                }
            }
        } else if (r > 0) {
            sndc_printf("FD SET return %d events\n", r);
            KAA_LOG_DEBUG(self->kaa_context->logger,
                    KAA_ERR_NONE,
                    "IO LOOP: select() return %d events", r);
            if (bootstrap_fd >= 0) {
                fdset = false;
                if (SNDC_FD_ISSET(bootstrap_fd, &r_set)) {
                    fdset = true;
                    KAA_LOG_DEBUG(self->kaa_context->logger,
                                        KAA_ERR_NONE,
                                        "IO LOOP: Read Event Bootstrap(%d)", bootstrap_fd);
                    error_code = kaa_tcp_channel_process_event(&self->bootstrap_channel, FD_READ);
                    if (error_code) {
                        KAA_LOG_ERROR(self->kaa_context->logger,
                                        error_code,
                                        "IO LOOP: Failed Read Event Bootstrap(%d)", bootstrap_fd);
                    }
                }
                if (fdset)
                    r--;
                if (r > 0) {
                    fdset = false;
                    if (SNDC_FD_ISSET(bootstrap_fd, &w_set)) {
                        fdset = true;
                        KAA_LOG_DEBUG(self->kaa_context->logger,
                                        KAA_ERR_NONE,
                                        "IO LOOP: Write Event Bootstrap(%d)", bootstrap_fd);
                        error_code = kaa_tcp_channel_process_event(&self->bootstrap_channel, FD_WRITE);
                        if (error_code) {
                            KAA_LOG_ERROR(self->kaa_context->logger,
                                        error_code,
                                        "IO LOOP: Failed Write Event Bootstrap(%d)", bootstrap_fd);
                        }
                    }
                    if (fdset)
                        r--;
                }
                if (r > 0) {
                    fdset = false;
                    if (SNDC_FD_ISSET(bootstrap_fd, &x_set)) {
                        fdset = true;
                        sndc_printf("Exception Event Bootstrap %d\n", bootstrap_fd);
                        KAA_LOG_DEBUG(self->kaa_context->logger,
                                        KAA_ERR_NONE,
                                        "IO LOOP: Exception Event Bootstrap(%d)", bootstrap_fd);
                        error_code = kaa_tcp_channel_process_event(&self->bootstrap_channel, FD_EXCEPTION);
                        if (error_code) {
                            KAA_LOG_ERROR(self->kaa_context->logger,
                                        error_code,
                                        "IO LOOP: Failed Exception Event Bootstrap(%d)", bootstrap_fd);
                        }
                    }
                    if (fdset)
                        r--;
                }
            }
            if (r > 0 && ops_fd >= 0) {
                fdset = false;
                if (SNDC_FD_ISSET(ops_fd, &r_set)) {
                    fdset = true;
                    KAA_LOG_DEBUG(self->kaa_context->logger,
                                    KAA_ERR_NONE,
                                    "IO LOOP: Read Event Operations(%d)", ops_fd);
                    error_code = kaa_tcp_channel_process_event(&self->operations_channel, FD_READ);
                    if (error_code) {
                        KAA_LOG_ERROR(self->kaa_context->logger,
                                    error_code,
                                    "IO LOOP: Failed Read Event Operations(%d)", ops_fd);
                    }
                }
                if (fdset)
                    r--;
                if (r > 0) {
                    fdset = false;
                    if (SNDC_FD_ISSET(ops_fd, &w_set)) {
                        fdset = true;
                        KAA_LOG_DEBUG(self->kaa_context->logger,
                                        KAA_ERR_NONE,
                                        "IO LOOP: Write Event Operations(%d)", ops_fd);
                        error_code = kaa_tcp_channel_process_event(&self->operations_channel, FD_WRITE);
                        if (error_code) {
                            KAA_LOG_ERROR(self->kaa_context->logger,
                                        error_code,
                                        "IO LOOP: Failed Write Event Operations(%d)", ops_fd);
                        }
                    }
                }
                if (fdset)
                    r--;
                if (r > 0) {
                    fdset = false;
                    if (SNDC_FD_ISSET(ops_fd, &x_set)) {
                        fdset = true;
                        sndc_printf("Exception Event Operations %d\n", ops_fd);
                        KAA_LOG_DEBUG(self->kaa_context->logger,
                                        KAA_ERR_NONE,
                                        "IO LOOP: Exception Event Operations(%d)", ops_fd);
                        error_code = kaa_tcp_channel_process_event(&self->operations_channel, FD_EXCEPTION);
                        if (error_code) {
                            KAA_LOG_ERROR(self->kaa_context->logger,
                                        error_code,
                                        "IO LOOP: Failed Exception Event Operations(%d)", ops_fd);
                        }
                    }
                }
            }
        } else {
            KAA_LOG_ERROR(self->kaa_context->logger,
                        KAA_ERR_BAD_STATE,
                        "IO LOOP: Error %d returned from select()", r);
        }
    }

    KAA_LOG_INFO(self->kaa_context->logger,
                    KAA_ERR_NONE,
                    "IO LOOP: Finished.");
}
Example #6
0
kaa_error_t kaa_client_create(kaa_client_t **kaa_client, kaa_client_props_t *props)
{
    KAA_RETURN_IF_NIL2(kaa_client, props, KAA_ERR_BADPARAM);

    kaa_error_t error_code = KAA_ERR_NONE;

    error_code = kaa_init_security_stuff();
    KAA_RETURN_IF_ERR(error_code);

    kaa_client_t *self = sndc_mem_calloc(1,sizeof(kaa_client_t));
    KAA_RETURN_IF_NIL(self,KAA_ERR_NOMEM);

    self->thread_name = sndc_mem_strdup("Kaa-Client-Thread");
    self->operate = true;
    self->max_update_time = props->max_update_time;

    print_mem_stat(self);
    sndc_printf("Initializing Kaa SDK...\n");
    sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);

    error_code = kaa_init(&self->kaa_context);
    if (error_code) {
        sndc_printf("Error during Kaa context creation %d\n", error_code);
        sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);
        goto error;
    }

    KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Kaa framework initialized.");

    print_mem_stat(self);
    error_code = kaa_log_collector_init(self);
    if (error_code) {
        sndc_printf("Failed to init Kaa log collector %d\n", error_code);
        sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);
        goto error;
    }

    KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Kaa log collector initialized.");

    *kaa_client = self;

    /* initialize semaphore */
    sndc_sem_init(&self->start_semophore, 0);
    sndc_sem_init(&self->logging_semophore, 0);

    int status = sndc_thrd_create(
            self->thread_name,
            thread_run_fn,
            (uintptr_t)self,
            SNDC_THRD_PRIORITY_DEFAULT,
            THREAD_STACKSIZE_DMR_START); //Defined 4K-8 bytes of stack

    switch (status) {
        case 0:

            break;
        case -EINVAL:
            error_code = KAA_ERR_BADPARAM;
            break;
        case -ENOMEM:
            error_code = KAA_ERR_NOMEM;
            break;
        default:
            error_code = KAA_ERR_BADDATA;
            break;
    }

    return error_code;

error:

    sndc_printf("Kaa initialization failed. error_code %d\n", error_code);
    sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);
    kaa_client_destroy(self);

    return error_code;
}
Example #7
0
static bool_t APP_handle_msg(sndc_appmsg_msg_t* msg)
{
   bool_t consumed = FALSE;
   switch(msg->id)
   {
      case SNDC_APPMSG_SCAN_COMPLETED:
      {
         sndc_printf("SNDC_APPMSG_SCAN_COMPLETED\n");
         break;
      }
      case SNDC_APPMSG_SCAN_IND:
      {
         sndc_printf("SNDC_APPMSG_SCAN_IND\n");
         break;
      }
      case SNDC_APPMSG_P2P_EVENT:
      {
         sndc_printf("SNDC_APPMSG_P2P_EVENT\n");
         break;
      }
      case SNDC_APPMSG_CM_EVENT:
      {
        sndc_printf("SNDC_APPMSG_CM_EVENT\n");
        sndc_appmsg_cmEvent_t *cm_event = (sndc_appmsg_cmEvent_t *)msg->par;

        sndc_printf("GOT SNDC_APPMSG_CM_EVENT FROM PROFILE ID: %d WITH ID: %d \n", cm_event->priv.profileID, cm_event->id);
        switch(cm_event->id)
        {
         case EVENT_TYPE_CM_PEER_STATUS:
         {
            switch(cm_event->priv.u.peer_status){
               case SNDC_CM_WLAN_DISCONNECTED: sndc_printf( "SNDC_CM_WLAN_DISCONNECTED\n"); break;
               case SNDC_CM_WLAN_CONNECT_TIMEOUT: sndc_printf( "SNDC_CM_WLAN_CONNECT_TIMEOUT\n"); break;
               case SNDC_CM_WLAN_CONNECT_FAILED: sndc_printf( "SNDC_CM_WLAN_CONNECT_FAILED\n"); break;
               case SNDC_CM_WLAN_WPS_FAILED: sndc_printf( "SNDC_CM_WLAN_WPS_FAILED\n"); break;
               case SNDC_CM_WLAN_WPS_TIMEOUT: sndc_printf( "SNDC_CM_WLAN_WPS_TIMEOUT\n"); break;
               case SNDC_CM_WLAN_CONNECTED: sndc_printf( "SNDC_CM_WLAN_CONNECTED\n"); break;
               default: sndc_printf( "[%d] peer_status:%d  \n",cm_event->priv.profileID, cm_event->priv.u.peer_status); break;
            }
            break;
         }
         case EVENT_TYPE_CM_CLIENT_STATUS:
            break;
         case EVENT_TYPE_CM_GO_STATUS:
            break;
         case EVENT_TYPE_CM_IP_ACQUIRED:
         {
            sndc_profile_ipConfig_t *ipconfig = cm_event->priv.u.ip_acquired.ipconfig;
            sndc_printf("**********************\n");
            sndc_printf("IP acquired, status = %d\n", cm_event->priv.u.ip_acquired.status);
            if(cm_event->priv.u.ip_acquired.status == 0)
            {
               assert(ipconfig);
               sndc_printf("IP addr: %s\n", sndc_inet_ntoa(ipconfig->ip_address));
               sndc_printf("Netmask: %s\n", sndc_inet_ntoa(ipconfig->netmask));
               sndc_printf("Gateway: %s\n", sndc_inet_ntoa(ipconfig->gateway));
               ip_connected = true;
               sndc_sys_initRandom(sndc_sys_getTimestamp_msec());
            }
            sndc_printf("**********************\n");


            break;
         }
         case EVENT_TYPE_CM_AUTO_MODE_STATE_CHANGED:
         {
            sndc_printf( "[%d] AutoMode state changed:%d  \n",cm_event->priv.profileID, cm_event->priv.u.auto_mode_state);
            break;
         }

        }

        break;
      }
      case SNDC_APPMSG_IO_EVENT:
      {
         sndc_printf("SNDC_APPMSG_IO_EVENT\n");
         sndc_appmsg_ioEvent_t *io_event = (sndc_appmsg_ioEvent_t *)msg->par;
         if (testDone) {
             sndc_printf("SNDC_APPMSG_IO_EVENT level %d, pinMask %d\n", io_event->level, io_event->pin_mask);
         } else {
             testDone  = true;
             sndc_printf("Button pressed. \n");
             start_client();
         }
         break;
      }

      default:
         break;
   }

   return consumed;
}
Example #8
0
static void APP_main()
{
   ip_connected = false;
   kaa_started = false;

   kaa_error_t kaa_error = KAA_ERR_NONE;
   { //Used to limit kaa_props visibility, it creates on stack and release once is used
       kaa_client_props_t kaa_props;
       kaa_props.max_update_time = 2;
       kaa_error = kaa_client_create(&kaa_client, &kaa_props);
       if (kaa_error) {
           sndc_printf("Error %d initializing Kaa client \n",kaa_error);
           return;
       }
   }

   kaa_list_t *zones = kaa_list_create();
   int i = 0;
   for(; i < LIGHT_ZONES_COUNT; ++i) {
       sndc_io_setMode(light_zones[i], IO_MODE_OUTPUT);
       sndc_io_write(light_zones[i], LIGHT_OFF);
       int32_t *zone_id = (int32_t *) KAA_MALLOC(sizeof(int32_t));
       *zone_id = i;
       kaa_list_push_front(zones, zone_id);
   }
   kaa_profile_street_lights_profile_t *profile = kaa_profile_street_lights_profile_create();
   profile->light_zones = zones;
   kaa_profile_manager_update_profile(kaa_client_get_context(kaa_client)->profile_manager, profile);
   profile->destroy(profile);

   /**
    * Configuration example, below is how to configure and read default configuration values
    */
   kaa_configuration_root_receiver_t receiver = { NULL, &kaa_on_configuration_updated };
   kaa_error = kaa_configuration_manager_set_root_receiver(kaa_client_get_context(kaa_client)->configuration_manager, &receiver);
   sndc_printf("Configuration setting done. %d\n", kaa_error);
   //sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);
   const kaa_root_configuration_t *root_config = kaa_configuration_manager_get_configuration(kaa_client_get_context(kaa_client)->configuration_manager);
   kaa_on_configuration_updated(NULL, root_config);

   //set SW2 button as key input
   //sndc_io_ctrl(BUTTON,
   //             IO_PIN_FUNC_PULL_UP,
   //             IO_PIN_DRIVE_DEFAULT,
   //             IO_PIN_SLEW_RATE_DEFAULT);
   //sndc_io_setMode(BUTTON, IO_MODE_KEY);

   //sndc_device_config = sndc_config_get();

   /* clean all profiles */
   //sndc_profile_eraseAll();
   //sndc_printf("Press SW2 to start test. \n");

   start_client();

   //infinite thread loop, button press is monitored by system events
   while(1)
   {

      if (ip_connected && !kaa_started) {
          kaa_client_start(kaa_client, NULL, NULL, 0);
          kaa_started = true;
      }
      //thread sleep for 500 ms
      sndc_thrd_delay(500 * SNDC_MILLISECOND);
   }
}