Github user arpadboda commented on a diff in the pull request: https://github.com/apache/nifi-minifi-cpp/pull/437#discussion_r233500619 --- Diff: extensions/coap/nanofi/coap_functions.c --- @@ -0,0 +1,175 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "coap_functions.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Initialize the API access. Not thread safe. + */ +void init_coap_api(void *rcvr, callback_pointers *ptrs) { + global_ptrs.data_received = ptrs->data_received; + global_ptrs.received_error = ptrs->received_error; + receiver = rcvr; +} + + +int create_session(coap_context_t **ctx, coap_session_t **session, const char *node, const char *port, coap_address_t *dst_addr) { + int s; + struct addrinfo hints; + coap_proto_t proto = COAP_PROTO_UDP; + struct addrinfo *result, *rp; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; // ipv4 or ipv6 + hints.ai_socktype = COAP_PROTO_RELIABLE(proto) ? SOCK_STREAM : SOCK_DGRAM; + hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL; + + s = getaddrinfo(node, port, &hints, &result); + if (s != 0) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + return -1; + } + + for (rp = result; rp != NULL; rp = rp->ai_next) { + coap_address_t addr; + + if (rp->ai_addrlen <= sizeof(addr.addr)) { + coap_address_init(&addr); + addr.size = rp->ai_addrlen; + memcpy(&addr.addr, rp->ai_addr, rp->ai_addrlen); + + *ctx = coap_new_context(0x00); + + *session = coap_new_client_session(*ctx, &addr, dst_addr, proto); + if (*ctx && *session) { + freeaddrinfo(result); + return 0; + } + } + } + + fprintf(stderr, "no context available for interface '%s'\n", node); + + freeaddrinfo(result); + return -1; +} + +struct coap_pdu_t *create_request(struct coap_context_t *ctx,struct coap_session_t *session,coap_optlist_t **optlist, unsigned char code, coap_str_const_t *ptr) { + coap_pdu_t *pdu; + + if (!(pdu = coap_new_pdu(session))) + return NULL; + + pdu->type = COAP_MESSAGE_CON; + pdu->tid = coap_new_message_id(session); + pdu->code = code; + + if (optlist){ + coap_add_optlist_pdu(pdu, optlist); + } + + int flags = 0; + coap_add_data(pdu, ptr->length, ptr->s); + return pdu; +} + +int coap_event(struct coap_context_t *ctx, coap_event_t event, struct coap_session_t *session){ + if (event == COAP_EVENT_SESSION_FAILED && global_ptrs.received_error){ --- End diff -- Please indent with 2 spaces
---