diff --git a/api/oc_discovery.c b/api/oc_discovery.c index 389388c4cb..1edd9febf7 100644 --- a/api/oc_discovery.c +++ b/api/oc_discovery.c @@ -637,12 +637,11 @@ oc_core_1_1_discovery_handler(oc_request_t *request, { (void)data; int matches = 0; - size_t device; switch (iface_mask) { case OC_IF_LL: { oc_rep_start_links_array(); - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { matches += process_oic_1_1_device_object(oc_rep_array(links), request, device, false); } @@ -650,7 +649,7 @@ oc_core_1_1_discovery_handler(oc_request_t *request, } break; case OC_IF_BASELINE: { oc_rep_start_links_array(); - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { matches += process_oic_1_1_device_object(oc_rep_array(links), request, device, true); } diff --git a/api/oc_main.c b/api/oc_main.c index c8908b5f3e..33b62b814c 100644 --- a/api/oc_main.c +++ b/api/oc_main.c @@ -251,8 +251,7 @@ oc_get_block_size(void) static void oc_shutdown_all_devices(void) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_connectivity_shutdown(device); } diff --git a/api/oc_resource_factory.c b/api/oc_resource_factory.c index 53862131a3..a354947bc9 100644 --- a/api/oc_resource_factory.c +++ b/api/oc_resource_factory.c @@ -208,7 +208,8 @@ oc_rt_factory_create_resource(oc_collection_t *collection, } void -oc_rt_factory_free_created_resource(oc_rt_created_t *rtc, oc_rt_factory_t *rf) +oc_rt_factory_free_created_resource(oc_rt_created_t *rtc, + const oc_rt_factory_t *rf) { if (oc_list_remove2(created_res, rtc) == NULL) { /* protection against cyclical call of oc_rt_factory_free_created_resource @@ -231,7 +232,7 @@ oc_fi_factory_free_all_created_resources(void) } oc_rt_created_t * -oc_rt_get_factory_create_for_resource(oc_resource_t *resource) +oc_rt_get_factory_create_for_resource(const oc_resource_t *resource) { oc_rt_created_t *rtc = (oc_rt_created_t *)oc_list_head(created_res); while (rtc) { diff --git a/api/oc_resource_factory.h b/api/oc_resource_factory.h index 334e29be4f..9de3e89adb 100644 --- a/api/oc_resource_factory.h +++ b/api/oc_resource_factory.h @@ -49,11 +49,12 @@ oc_rt_created_t *oc_rt_factory_create_resource(oc_collection_t *collection, size_t device); void oc_rt_factory_free_created_resource(oc_rt_created_t *rtc, - oc_rt_factory_t *rf); + const oc_rt_factory_t *rf); void oc_rt_factory_free_created_resources(size_t device); -oc_rt_created_t *oc_rt_get_factory_create_for_resource(oc_resource_t *resource); +oc_rt_created_t *oc_rt_get_factory_create_for_resource( + const oc_resource_t *resource); void oc_fi_factory_free_all_created_resources(void); diff --git a/api/oc_server_api.c b/api/oc_server_api.c index 876913e6bf..fbdd97e66c 100644 --- a/api/oc_server_api.c +++ b/api/oc_server_api.c @@ -225,7 +225,7 @@ oc_reset_delayed_callback_ms(void *cb_data, oc_trigger_t callback, } bool -oc_has_delayed_callback(void *cb_data, oc_trigger_t callback, +oc_has_delayed_callback(const void *cb_data, oc_trigger_t callback, bool ignore_cb_data) { return oc_ri_has_timed_event_callback(cb_data, callback, ignore_cb_data); @@ -242,7 +242,7 @@ oc_remove_delayed_callback_by_filter(oc_trigger_t cb, } void -oc_remove_delayed_callback(void *cb_data, oc_trigger_t callback) +oc_remove_delayed_callback(const void *cb_data, oc_trigger_t callback) { oc_ri_remove_timed_event_callback(cb_data, callback); } @@ -653,8 +653,7 @@ oc_resource_set_secure_mcast(oc_resource_t *resource, bool supported) void oc_set_con_write_cb(oc_con_write_cb_t callback) { - size_t i; - for (i = 0; i < oc_core_get_num_devices(); i++) { + for (size_t i = 0; i < oc_core_get_num_devices(); i++) { oc_resource_t *res = oc_core_get_resource_by_index(OCF_CON, i); if (res) { res->post_handler.user_data = *(void **)(&callback); diff --git a/apps/client_collections_linux.c b/apps/client_collections_linux.c index 51bc45c84c..08042b6307 100644 --- a/apps/client_collections_linux.c +++ b/apps/client_collections_linux.c @@ -79,15 +79,14 @@ post_lights_oic_if_create(oc_client_response_t *data) case OC_REP_STRING: OC_PRINTF("%s\n\n", oc_string(rep->value.string)); break; - case OC_REP_STRING_ARRAY: { - size_t i; - for (i = 0; i < oc_string_array_get_allocated_size(rep->value.array); - i++) { + case OC_REP_STRING_ARRAY: + for (size_t i = 0; + i < oc_string_array_get_allocated_size(rep->value.array); i++) { OC_PRINTF(" %s ", oc_string_array_get_item(rep->value.array, i)); } OC_PRINTF("\n"); - } break; + break; case OC_REP_INT: OC_PRINTF(" %" PRId64 "\n", rep->value.integer); break; diff --git a/apps/cloud_server.c b/apps/cloud_server.c index d39b9e0bf7..62127c6870 100644 --- a/apps/cloud_server.c +++ b/apps/cloud_server.c @@ -606,35 +606,36 @@ get_switch_instance(const char *href, oc_string_array_t *types, size_t device) { oc_switch_t *cswitch = (oc_switch_t *)oc_memb_alloc(&switch_s); - if (cswitch) { - cswitch->resource = oc_new_resource( - NULL, href, oc_string_array_get_allocated_size(*types), device); - if (cswitch->resource) { - size_t i; - for (i = 0; i < oc_string_array_get_allocated_size(*types); i++) { - const char *rt = oc_string_array_get_item(*types, i); - oc_resource_bind_resource_type(cswitch->resource, rt); - } - oc_resource_bind_resource_interface(cswitch->resource, iface_mask); - cswitch->resource->properties = bm; - oc_resource_set_default_interface(cswitch->resource, OC_IF_A); - oc_resource_set_request_handler(cswitch->resource, OC_GET, get_cswitch, - cswitch); - oc_resource_set_request_handler(cswitch->resource, OC_DELETE, - delete_cswitch, cswitch); - oc_resource_set_request_handler(cswitch->resource, OC_POST, post_cswitch, - cswitch); - oc_resource_set_properties_cbs(cswitch->resource, get_switch_properties, - cswitch, set_switch_properties, cswitch); - oc_add_resource(cswitch->resource); - oc_set_delayed_callback(cswitch->resource, register_to_cloud, 0); - oc_list_add(switches, cswitch); - return cswitch->resource; - } else { - oc_memb_free(&switch_s, cswitch); - } - } - return NULL; + if (cswitch == NULL) { + printf("ERROR: insufficient memory to add new switch instance"); + return NULL; + } + cswitch->resource = oc_new_resource( + NULL, href, oc_string_array_get_allocated_size(*types), device); + if (cswitch->resource == NULL) { + printf("ERROR: could not create /switch instance"); + oc_memb_free(&switch_s, cswitch); + return NULL; + } + for (size_t i = 0; i < oc_string_array_get_allocated_size(*types); i++) { + const char *rt = oc_string_array_get_item(*types, i); + oc_resource_bind_resource_type(cswitch->resource, rt); + } + oc_resource_bind_resource_interface(cswitch->resource, iface_mask); + cswitch->resource->properties = bm; + oc_resource_set_default_interface(cswitch->resource, OC_IF_A); + oc_resource_set_request_handler(cswitch->resource, OC_GET, get_cswitch, + cswitch); + oc_resource_set_request_handler(cswitch->resource, OC_DELETE, delete_cswitch, + cswitch); + oc_resource_set_request_handler(cswitch->resource, OC_POST, post_cswitch, + cswitch); + oc_resource_set_properties_cbs(cswitch->resource, get_switch_properties, + cswitch, set_switch_properties, cswitch); + oc_add_resource(cswitch->resource); + oc_set_delayed_callback(cswitch->resource, register_to_cloud, 0); + oc_list_add(switches, cswitch); + return cswitch->resource; } static void diff --git a/include/oc_api.h b/include/oc_api.h index b0f1d6f00e..bf8b65fe7b 100644 --- a/include/oc_api.h +++ b/include/oc_api.h @@ -2645,7 +2645,7 @@ void oc_set_delayed_callback_ms_v1(void *cb_data, oc_trigger_t callback, * @return false otherwise */ OC_API -bool oc_has_delayed_callback(void *cb_data, oc_trigger_t callback, +bool oc_has_delayed_callback(const void *cb_data, oc_trigger_t callback, bool ignore_cb_data); /** @@ -2681,7 +2681,7 @@ void oc_remove_delayed_callback_by_filter( * @param[in] callback the delayed callback that is being removed */ OC_API -void oc_remove_delayed_callback(void *cb_data, oc_trigger_t callback); +void oc_remove_delayed_callback(const void *cb_data, oc_trigger_t callback); /** API for setting handlers for interrupts */ diff --git a/messaging/coap/engine.c b/messaging/coap/engine.c index 57e79c484f..85d513419f 100644 --- a/messaging/coap/engine.c +++ b/messaging/coap/engine.c @@ -97,8 +97,7 @@ static uint8_t g_idx = 0; bool oc_coap_check_if_duplicate(uint16_t mid, uint32_t device) { - size_t i; - for (i = 0; i < OC_REQUEST_HISTORY_SIZE; i++) { + for (size_t i = 0; i < OC_REQUEST_HISTORY_SIZE; i++) { if (g_history[i] == mid && g_history_dev[i] == device) { OC_DBG("dropping duplicate request"); return true; diff --git a/messaging/coap/oscore.c b/messaging/coap/oscore.c index b772c59828..3758c97b77 100644 --- a/messaging/coap/oscore.c +++ b/messaging/coap/oscore.c @@ -69,8 +69,8 @@ oscore_read_piv(const uint8_t *piv, uint8_t piv_len, uint64_t *ssn) { *ssn = 0; - uint8_t i, j = sizeof(uint64_t) - piv_len; - for (i = 0; i < piv_len; i++, j++) { + uint8_t j = sizeof(uint64_t) - piv_len; + for (uint8_t i = 0; i < piv_len; i++, j++) { memcpy((char *)ssn + j, &piv[i], 1); } diff --git a/messaging/coap/transactions.c b/messaging/coap/transactions.c index 16e7fb8a0e..84e69ec46d 100644 --- a/messaging/coap/transactions.c +++ b/messaging/coap/transactions.c @@ -211,10 +211,9 @@ coap_clear_transaction(coap_transaction_t *t) coap_transaction_t * coap_get_transaction_by_mid(uint16_t mid) { - coap_transaction_t *t = NULL; - - for (t = (coap_transaction_t *)oc_list_head(transactions_list); t; - t = t->next) { + for (coap_transaction_t *t = + (coap_transaction_t *)oc_list_head(transactions_list); + t != NULL; t = t->next) { if (t->mid == mid) { OC_DBG("Found transaction for MID %u: %p", t->mid, (void *)t); return t; @@ -226,10 +225,9 @@ coap_get_transaction_by_mid(uint16_t mid) coap_transaction_t * coap_get_transaction_by_token(uint8_t *token, uint8_t token_len) { - coap_transaction_t *t = NULL; - - for (t = (coap_transaction_t *)oc_list_head(transactions_list); t; - t = t->next) { + for (coap_transaction_t *t = + (coap_transaction_t *)oc_list_head(transactions_list); + t != NULL; t = t->next) { if (t->token_len == token_len && memcmp(t->token, token, token_len) == 0) { OC_DBG("Found transaction by token %p", (void *)t); return t; @@ -241,10 +239,9 @@ coap_get_transaction_by_token(uint8_t *token, uint8_t token_len) void coap_check_transactions(void) { - coap_transaction_t *t = (coap_transaction_t *)oc_list_head(transactions_list), - *next; + coap_transaction_t *t = (coap_transaction_t *)oc_list_head(transactions_list); while (t != NULL) { - next = t->next; + coap_transaction_t *next = t->next; if (oc_etimer_expired(&t->retrans_timer)) { ++(t->retrans_counter); OC_DBG("Retransmitting %u (%u)", t->mid, t->retrans_counter); @@ -262,10 +259,9 @@ coap_check_transactions(void) void coap_free_all_transactions(void) { - coap_transaction_t *t = (coap_transaction_t *)oc_list_head(transactions_list), - *next; + coap_transaction_t *t = (coap_transaction_t *)oc_list_head(transactions_list); while (t != NULL) { - next = t->next; + coap_transaction_t *next = t->next; coap_clear_transaction(t); t = next; } diff --git a/port/linux/ipadapter.c b/port/linux/ipadapter.c index 4083c1fd78..2b8fdfec8a 100644 --- a/port/linux/ipadapter.c +++ b/port/linux/ipadapter.c @@ -127,12 +127,13 @@ add_ip_interface(int target_index) static bool check_new_ip_interfaces(void) { - struct ifaddrs *ifs = NULL, *interface = NULL; + struct ifaddrs *ifs = NULL; if (getifaddrs(&ifs) < 0) { OC_ERR("querying interface address"); return false; } - for (interface = ifs; interface != NULL; interface = interface->ifa_next) { + for (struct ifaddrs *interface = ifs; interface != NULL; + interface = interface->ifa_next) { /* Ignore interfaces that are down and the loopback interface */ if (!(interface->ifa_flags & IFF_UP) || (interface->ifa_flags & IFF_LOOPBACK)) { diff --git a/security/oc_ael.c b/security/oc_ael.c index 7b2090b83c..5eb26256f2 100644 --- a/security/oc_ael.c +++ b/security/oc_ael.c @@ -122,8 +122,7 @@ oc_sec_ael_init(void) oc_abort("Insufficient memory"); } #endif /* OC_DYNAMIC_ALLOCATION */ - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { OC_LIST_STRUCT_INIT(&ael[device], events); } } @@ -131,8 +130,7 @@ oc_sec_ael_init(void) void oc_sec_ael_free(void) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_sec_ael_reset(device); } #ifdef OC_DYNAMIC_ALLOCATION @@ -492,8 +490,7 @@ oc_sec_ael_create_event(size_t device, uint8_t category, uint8_t priority, oc_new_string(&res->message, message, strlen(message)); } if (aux_info && aux_size > 0) { - size_t i; - for (i = 0; i < aux_size; i++) { + for (size_t i = 0; i < aux_size; i++) { oc_sec_ael_aux_info_t *a_info = (oc_sec_ael_aux_info_t *)oc_memb_alloc(&aux_s); if (a_info) { diff --git a/security/oc_cred.c b/security/oc_cred.c index f4aafe8bac..82b8642dee 100644 --- a/security/oc_cred.c +++ b/security/oc_cred.c @@ -95,8 +95,7 @@ oc_sec_cred_init(void) oc_abort("Insufficient memory"); } #endif /* OC_DYNAMIC_ALLOCATION */ - size_t i; - for (i = 0; i < oc_core_get_num_devices(); i++) { + for (size_t i = 0; i < oc_core_get_num_devices(); i++) { OC_LIST_STRUCT_INIT(&devices[i], creds); } } @@ -319,8 +318,7 @@ oc_sec_cred_default(size_t device) void oc_sec_cred_free(void) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_sec_cred_clear(device, NULL, NULL); } #ifdef OC_DYNAMIC_ALLOCATION @@ -1248,8 +1246,7 @@ is_valid_oscore_id(const char *id, size_t id_len) if (id_len != 14) { return false; } - size_t i; - for (i = 0; i < id_len; i++) { + for (size_t i = 0; i < id_len; i++) { if (!isxdigit(id[i])) { return false; } diff --git a/security/oc_oscore_engine.c b/security/oc_oscore_engine.c index 534a98acbf..d2aaafb566 100644 --- a/security/oc_oscore_engine.c +++ b/security/oc_oscore_engine.c @@ -50,12 +50,11 @@ dump_cred(void *data) static bool check_if_replayed_request(oc_oscore_context_t *oscore_ctx, uint64_t piv) { - uint8_t i; if (piv == 0 && oscore_ctx->rwin[0] == 0 && oscore_ctx->rwin[OSCORE_REPLAY_WINDOW_SIZE - 1] == 0) { goto fresh_request; } - for (i = 0; i < OSCORE_REPLAY_WINDOW_SIZE; i++) { + for (uint8_t i = 0; i < OSCORE_REPLAY_WINDOW_SIZE; i++) { if (oscore_ctx->rwin[i] == piv) { return true; } diff --git a/security/oc_pstat.c b/security/oc_pstat.c index c17a3c4215..533f909e81 100644 --- a/security/oc_pstat.c +++ b/security/oc_pstat.c @@ -675,8 +675,7 @@ oc_reset_device_v1(size_t device, bool close_all_tls_connections_immediately) void oc_reset_v1(bool close_all_tls_connections_immediately) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_reset_device_v1(device, close_all_tls_connections_immediately); } } diff --git a/security/oc_tls.c b/security/oc_tls.c index 8e24ef16bc..1054922af4 100644 --- a/security/oc_tls.c +++ b/security/oc_tls.c @@ -830,8 +830,7 @@ oc_tls_add_new_certs(oc_sec_credusage_t credusage, check_if_known_cert_cb is_known_cert, add_new_cert_cb add_new_cert) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_sec_creds_t *creds = oc_sec_get_creds(device); oc_sec_cred_t *cred = (oc_sec_cred_t *)oc_list_head(creds->creds); for (; cred != NULL; cred = cred->next) { diff --git a/security/unittest/tls_cert_test.cpp b/security/unittest/tls_cert_test.cpp index 0be0ef6783..0fe52d5409 100644 --- a/security/unittest/tls_cert_test.cpp +++ b/security/unittest/tls_cert_test.cpp @@ -179,8 +179,7 @@ TEST_F(TestTlsCertificates, RemoveTrustAnchors) TEST_F(TestTlsCertificates, VerifyCredCerts) { - auto verify_cert_validity = [](const oc_sec_certs_data_t *data, - void *) -> bool { + auto verify_cert_validity = [](const oc_sec_certs_data_t *data, void *) { return (time_t)data->valid_from <= TestTlsCertificates::now_ && (time_t)data->valid_to > TestTlsCertificates::now_; };