NanoService device game controller for NSPong.
Dependencies: Beep C12832_lcd EthernetInterface MMA7660 mbed-rtos mbed nsdl_lib
nsdl_support.cpp
00001 // NSDL library support functions 00002 00003 #include "mbed.h" 00004 #include "nsdl_support.h" 00005 #include "mbed.h" 00006 #include "rtos.h" 00007 #include "EthernetInterface.h" 00008 00009 extern Serial pc; 00010 extern EthernetInterface eth; 00011 extern Endpoint nsp; 00012 extern UDPSocket server; 00013 extern char endpoint_name[16]; 00014 extern uint8_t ep_type[]; 00015 extern uint8_t lifetime_ptr[]; 00016 00017 /* The number of seconds between NSP registration messages */ 00018 #define RD_UPDATE_PERIOD 60 00019 00020 void *nsdl_alloc(uint16_t size) 00021 { 00022 return malloc(size); 00023 } 00024 00025 void nsdl_free(void* ptr_to_free) 00026 { 00027 free(ptr_to_free); 00028 } 00029 00030 /* 00031 * Create a static resoure 00032 * Only get is allowed 00033 */ 00034 void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len) 00035 { 00036 resource_structure->access = SN_GRS_GET_ALLOWED; 00037 resource_structure->mode = SN_GRS_STATIC; 00038 resource_structure->pathlen = pt_len; 00039 resource_structure->path = pt; 00040 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; 00041 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; 00042 resource_structure->resource = rsc; 00043 resource_structure->resourcelen = rsc_len; 00044 sn_nsdl_create_resource(resource_structure); 00045 } 00046 00047 void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right) 00048 { 00049 resource_structure->access = (sn_grs_resource_acl_e)access_right; 00050 resource_structure->resource = 0; 00051 resource_structure->resourcelen = 0; 00052 resource_structure->sn_grs_dyn_res_callback = callback_ptr; 00053 resource_structure->mode = SN_GRS_DYNAMIC; 00054 resource_structure->pathlen = pt_len; 00055 resource_structure->path = pt; 00056 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; 00057 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; 00058 resource_structure->resource_parameters_ptr->observable = is_observable; 00059 sn_nsdl_create_resource(resource_structure); 00060 } 00061 00062 sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr) 00063 { 00064 if (NULL == endpoint_structure) 00065 { 00066 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); 00067 } 00068 if (endpoint_structure) 00069 { 00070 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); 00071 uint8_t domain_name[5] = "pong"; 00072 endpoint_structure->endpoint_name_ptr = name; 00073 endpoint_structure->endpoint_name_len = strlen((char*)name); 00074 endpoint_structure->domain_name_ptr = domain_name; 00075 endpoint_structure->domain_name_len = strlen((char*)domain_name); 00076 endpoint_structure->type_ptr = typename_ptr; 00077 endpoint_structure->type_len = strlen((char*)typename_ptr); 00078 endpoint_structure->lifetime_ptr = lifetime_ptr; 00079 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); 00080 } 00081 return endpoint_structure; 00082 } 00083 00084 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) 00085 { 00086 if (*endpoint_structure) 00087 { 00088 nsdl_free(*endpoint_structure); 00089 *endpoint_structure = NULL; 00090 } 00091 } 00092 00093 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) 00094 { 00095 //pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len); 00096 00097 if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) 00098 pc.printf("sending failed\n\r"); 00099 00100 return 1; 00101 } 00102 00103 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) 00104 { 00105 pc.printf("RX callback!\r\n"); 00106 return 0; 00107 } 00108 00109 static void registration_update_thread(void const *args) 00110 { 00111 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; 00112 00113 while(true) 00114 { 00115 wait(RD_UPDATE_PERIOD); 00116 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); 00117 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) 00118 pc.printf("NSP re-registering failed\r\n"); 00119 else 00120 pc.printf("NSP re-registering OK\r\n"); 00121 nsdl_clean_register_endpoint(&endpoint_ptr); 00122 } 00123 } 00124 00125 void nsdl_init() 00126 { 00127 uint8_t nsp_addr[4]; 00128 sn_nsdl_mem_s memory_cbs; 00129 00130 /* Initialize libNsdl */ 00131 memory_cbs.sn_nsdl_alloc = &nsdl_alloc; 00132 memory_cbs.sn_nsdl_free = &nsdl_free; 00133 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) 00134 pc.printf("libNsdl init failed\r\n"); 00135 else 00136 pc.printf("libNsdl init done\r\n"); 00137 00138 /* Set nsp address for library */ 00139 set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); 00140 } 00141 00142 void nsdl_event_loop() 00143 { 00144 // Re-registration thread disabled 00145 //Thread registration_thread(registration_update_thread); 00146 00147 sn_nsdl_addr_s received_packet_address; 00148 uint8_t received_address[4]; 00149 char buffer[1024]; 00150 Endpoint from; 00151 00152 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); 00153 received_packet_address.addr_ptr = received_address; 00154 00155 while(1) 00156 { 00157 int n = server.receiveFrom(from, buffer, sizeof(buffer)); 00158 if (n < 0) 00159 { 00160 pc.printf("Socket error\n\r"); 00161 } 00162 else 00163 { 00164 pc.printf("Received %d bytes\r\n", n); 00165 sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); 00166 } 00167 } 00168 } 00169 00170
Generated on Wed Jul 13 2022 01:21:07 by 1.7.2