net: lib: coap: Use coap_transmission_parameters in coap_server

Update coap_service_send and coap_resource_send to take an optional
pointer argument to the newly introduced coap_transmission_parameters.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2023-12-14 15:53:28 +01:00 committed by Carles Cufí
parent eaba47445a
commit 4807ada01e
11 changed files with 29 additions and 33 deletions

View File

@ -97,7 +97,7 @@ The following is an example of a CoAP resource registered with our service:
coap_packet_append_payload(&response, (uint8_t *)msg, sizeof(msg));
/* Send to response back to the client */
return coap_resource_send(resource, &response, addr, addr_len);
return coap_resource_send(resource, &response, addr, addr_len, NULL);
}
static int my_put(struct coap_resource *resource, struct coap_packet *request,
@ -178,7 +178,7 @@ of CoAP services. An example using a temperature sensor can look like:
coap_packet_append_payload_marker(&response);
coap_packet_append_payload(&response, (uint8_t *)payload, strlen(payload));
return coap_resource_send(resource, &response, addr, addr_len);
return coap_resource_send(resource, &response, addr, addr_len, NULL);
}
static int temp_get(struct coap_resource *resource, struct coap_packet *request,

View File

@ -235,10 +235,12 @@ int coap_service_is_running(const struct coap_service *service);
* @param cpkt CoAP Packet to send
* @param addr Peer address
* @param addr_len Peer address length
* @param params Pointer to transmission parameters structure or NULL to use default values.
* @return 0 in case of success or negative in case of error.
*/
int coap_service_send(const struct coap_service *service, const struct coap_packet *cpkt,
const struct sockaddr *addr, socklen_t addr_len);
const struct sockaddr *addr, socklen_t addr_len,
const struct coap_transmission_parameters *params);
/**
* @brief Send a CoAP message from the provided @p resource .
@ -249,10 +251,12 @@ int coap_service_send(const struct coap_service *service, const struct coap_pack
* @param cpkt CoAP Packet to send
* @param addr Peer address
* @param addr_len Peer address length
* @param params Pointer to transmission parameters structure or NULL to use default values.
* @return 0 in case of success or negative in case of error.
*/
int coap_resource_send(const struct coap_resource *resource, const struct coap_packet *cpkt,
const struct sockaddr *addr, socklen_t addr_len);
const struct sockaddr *addr, socklen_t addr_len,
const struct coap_transmission_parameters *params);
/**
* @brief Parse a CoAP observe request for the provided @p resource .

View File

@ -45,7 +45,7 @@ static int core_get(struct coap_resource *resource,
return r;
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}

View File

@ -87,7 +87,7 @@ static int large_get(struct coap_resource *resource,
memset(&ctx, 0, sizeof(ctx));
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}
@ -167,7 +167,7 @@ static int large_update_put(struct coap_resource *resource,
return r;
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}
@ -239,7 +239,7 @@ static int large_create_post(struct coap_resource *resource,
return r;
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}

View File

@ -59,7 +59,7 @@ static int location_query_post(struct coap_resource *resource,
}
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}

View File

@ -80,7 +80,7 @@ static int send_notification_packet(struct coap_resource *resource,
k_work_reschedule(&obs_work, K_SECONDS(5));
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}

View File

@ -89,7 +89,7 @@ static int query_get(struct coap_resource *resource,
return r;
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}

View File

@ -43,7 +43,7 @@ static int separate_get(struct coap_resource *resource,
return r;
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
if (r < 0) {
return r;
}
@ -86,7 +86,7 @@ static int separate_get(struct coap_resource *resource,
return r;
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}

View File

@ -73,7 +73,7 @@ static int piggyback_get(struct coap_resource *resource,
return r;
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}
@ -113,7 +113,7 @@ static int test_del(struct coap_resource *resource,
return r;
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}
@ -160,7 +160,7 @@ static int test_put(struct coap_resource *resource,
return r;
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}
@ -221,7 +221,7 @@ static int test_post(struct coap_resource *resource,
}
}
r = coap_resource_send(resource, &response, addr, addr_len);
r = coap_resource_send(resource, &response, addr, addr_len, NULL);
return r;
}

View File

@ -202,12 +202,6 @@ config COAP_SERVICE_PENDING_MESSAGES
help
Maximum number of pending CoAP messages to retransmit per active service.
config COAP_SERVICE_PENDING_RETRANSMITS
int "CoAP retransmit count"
default 2
help
Maximum number of retries to send a pending message.
config COAP_SERVICE_OBSERVERS
int "CoAP service observers"
default 3

View File

@ -215,7 +215,7 @@ static int coap_server_process(int sock_fd)
goto unlock;
}
ret = coap_service_send(service, &response, &client_addr, client_addr_len);
ret = coap_service_send(service, &response, &client_addr, client_addr_len, NULL);
} else {
ret = coap_handle_request_len(&request, service->res_begin,
COAP_SERVICE_RESOURCE_COUNT(service),
@ -246,7 +246,7 @@ static int coap_server_process(int sock_fd)
goto unlock;
}
ret = coap_service_send(service, &ack, &client_addr, client_addr_len);
ret = coap_service_send(service, &ack, &client_addr, client_addr_len, NULL);
}
}
@ -521,7 +521,8 @@ int coap_service_is_running(const struct coap_service *service)
}
int coap_service_send(const struct coap_service *service, const struct coap_packet *cpkt,
const struct sockaddr *addr, socklen_t addr_len)
const struct sockaddr *addr, socklen_t addr_len,
const struct coap_transmission_parameters *params)
{
int ret;
@ -542,8 +543,6 @@ int coap_service_send(const struct coap_service *service, const struct coap_pack
* try to send.
*/
if (coap_header_get_type(cpkt) == COAP_TYPE_CON) {
struct coap_transmission_parameters params;
struct coap_pending *pending = coap_pending_next_unused(service->data->pending,
MAX_PENDINGS);
@ -552,9 +551,7 @@ int coap_service_send(const struct coap_service *service, const struct coap_pack
goto send;
}
params = coap_get_transmission_parameters();
params.max_retransmission = CONFIG_COAP_SERVICE_PENDING_RETRANSMITS;
ret = coap_pending_init(pending, cpkt, addr, &params);
ret = coap_pending_init(pending, cpkt, addr, params);
if (ret < 0) {
LOG_WRN("Failed to init pending message for %s (%d)", service->name, ret);
goto send;
@ -589,12 +586,13 @@ send:
}
int coap_resource_send(const struct coap_resource *resource, const struct coap_packet *cpkt,
const struct sockaddr *addr, socklen_t addr_len)
const struct sockaddr *addr, socklen_t addr_len,
const struct coap_transmission_parameters *params)
{
/* Find owning service */
COAP_SERVICE_FOREACH(svc) {
if (COAP_SERVICE_HAS_RESOURCE(svc, resource)) {
return coap_service_send(svc, cpkt, addr, addr_len);
return coap_service_send(svc, cpkt, addr, addr_len, params);
}
}