Skip to content

Commit

Permalink
Support for multiple virtual services (ARMmbed#58)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Arto Kinnunen authored Jan 16, 2017
1 parent 7fe6b98 commit 6a5634a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions source/coap_connection_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef struct internal_socket_s {
int16_t data_len;
uint8_t *data;

int8_t socket;
int8_t socket; //positive value = socket id, negative value virtual socket id
bool real_socket;
uint8_t usage_counter;
bool is_secure;
Expand Down Expand Up @@ -116,6 +116,17 @@ static void secure_session_delete(secure_session_t *this)
return;
}

static int8_t virtual_socket_id_allocate()
{
int8_t new_virtual_socket_id = -1; // must not overlap with real socket id's
ns_list_foreach(internal_socket_t, cur_ptr, &socket_list) {
if (cur_ptr->socket <= new_virtual_socket_id) {
new_virtual_socket_id = cur_ptr->socket - 1;
}
}
return new_virtual_socket_id;
}

static secure_session_t *secure_session_create(internal_socket_t *parent, const uint8_t *address_ptr, uint16_t port)
{
if(!address_ptr){
Expand Down Expand Up @@ -245,8 +256,8 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
// Set socket option to receive packet info
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_RECVPKTINFO, &(const bool) {1}, sizeof(bool));

}else{
this->socket = -1;
} else {
this->socket = virtual_socket_id_allocate();
}

ns_list_add_to_start(&socket_list, this);
Expand Down
4 changes: 2 additions & 2 deletions source/coap_service_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static int send_cb(int8_t socket_id, const uint8_t address[static 16], uint16_t
{
coap_service_t *this = service_find_by_socket(socket_id);
if (this && this->virtual_socket_send_cb) {
tr_debug("send to virtual socket");
tr_debug("send to virtual socket, service: %d", this->service_id);
return this->virtual_socket_send_cb(this->service_id, (uint8_t*)address, port, data_ptr, data_len);
}
return -1;
Expand Down Expand Up @@ -380,7 +380,7 @@ int16_t coap_service_virtual_socket_recv(int8_t service_id, uint8_t source_addr_
int16_t coap_service_virtual_socket_set_cb(int8_t service_id, coap_service_virtual_socket_send_cb *send_method_ptr)
{
coap_service_t *this = service_find(service_id);
tr_debug("register virtual socket cb");
tr_debug("register virtual socket cb to service %d", service_id);
if (!this) {
return -1;
}
Expand Down

0 comments on commit 6a5634a

Please sign in to comment.