Skip to content

Commit 6a5634a

Browse files
author
Arto Kinnunen
authored
Support for multiple virtual services (#58)
Socket id is initialized to -1 when virtual service is used. Therefore it is not possible to search virtual service based on socket id. Use negative socket id's to identify virtual services. share same socket ID coap-service supports one virtual socket.
1 parent 7fe6b98 commit 6a5634a

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

source/coap_connection_handler.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef struct internal_socket_s {
3232
int16_t data_len;
3333
uint8_t *data;
3434

35-
int8_t socket;
35+
int8_t socket; //positive value = socket id, negative value virtual socket id
3636
bool real_socket;
3737
uint8_t usage_counter;
3838
bool is_secure;
@@ -116,6 +116,17 @@ static void secure_session_delete(secure_session_t *this)
116116
return;
117117
}
118118

119+
static int8_t virtual_socket_id_allocate()
120+
{
121+
int8_t new_virtual_socket_id = -1; // must not overlap with real socket id's
122+
ns_list_foreach(internal_socket_t, cur_ptr, &socket_list) {
123+
if (cur_ptr->socket <= new_virtual_socket_id) {
124+
new_virtual_socket_id = cur_ptr->socket - 1;
125+
}
126+
}
127+
return new_virtual_socket_id;
128+
}
129+
119130
static secure_session_t *secure_session_create(internal_socket_t *parent, const uint8_t *address_ptr, uint16_t port)
120131
{
121132
if(!address_ptr){
@@ -245,8 +256,8 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
245256
// Set socket option to receive packet info
246257
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_RECVPKTINFO, &(const bool) {1}, sizeof(bool));
247258

248-
}else{
249-
this->socket = -1;
259+
} else {
260+
this->socket = virtual_socket_id_allocate();
250261
}
251262

252263
ns_list_add_to_start(&socket_list, this);

source/coap_service_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ static int send_cb(int8_t socket_id, const uint8_t address[static 16], uint16_t
235235
{
236236
coap_service_t *this = service_find_by_socket(socket_id);
237237
if (this && this->virtual_socket_send_cb) {
238-
tr_debug("send to virtual socket");
238+
tr_debug("send to virtual socket, service: %d", this->service_id);
239239
return this->virtual_socket_send_cb(this->service_id, (uint8_t*)address, port, data_ptr, data_len);
240240
}
241241
return -1;
@@ -380,7 +380,7 @@ int16_t coap_service_virtual_socket_recv(int8_t service_id, uint8_t source_addr_
380380
int16_t coap_service_virtual_socket_set_cb(int8_t service_id, coap_service_virtual_socket_send_cb *send_method_ptr)
381381
{
382382
coap_service_t *this = service_find(service_id);
383-
tr_debug("register virtual socket cb");
383+
tr_debug("register virtual socket cb to service %d", service_id);
384384
if (!this) {
385385
return -1;
386386
}

0 commit comments

Comments
 (0)