From c69f318e8cbdb8124e40e6a47b1bbffe35914a44 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Tue, 1 Nov 2016 14:32:47 +0100 Subject: lib: Simplify CDAP API This will simplify the CDAP API. Now the opcode has to be given when sending a CDAP request. Before a separate operation was provided since some of the function parameters are unused for certain commands. --- src/ipcpd/normal/cdap_request.h | 9 -- src/ipcpd/normal/ribmgr.c | 136 +++++++++++++--------------- src/lib/cdap.c | 193 ++++++++++++++-------------------------- 3 files changed, 129 insertions(+), 209 deletions(-) (limited to 'src') diff --git a/src/ipcpd/normal/cdap_request.h b/src/ipcpd/normal/cdap_request.h index b3be44f8..9cccfda5 100644 --- a/src/ipcpd/normal/cdap_request.h +++ b/src/ipcpd/normal/cdap_request.h @@ -30,15 +30,6 @@ #include -enum cdap_opcode { - READ = 0, - WRITE, - START, - STOP, - CREATE, - DELETE -}; - enum creq_state { REQ_INIT = 0, REQ_PENDING, diff --git a/src/ipcpd/normal/ribmgr.c b/src/ipcpd/normal/ribmgr.c index 295a6724..60872ef2 100644 --- a/src/ipcpd/normal/ribmgr.c +++ b/src/ipcpd/normal/ribmgr.c @@ -165,11 +165,11 @@ int ribmgr_fini() return 0; } -int ribmgr_cdap_reply(struct cdap * instance, - int invoke_id, - int result, - uint8_t * data, - size_t len) +static int ribmgr_cdap_reply(struct cdap * instance, + int invoke_id, + int result, + uint8_t * data, + size_t len) { struct list_head * pos, * n = NULL; @@ -206,24 +206,12 @@ int ribmgr_cdap_reply(struct cdap * instance, return 0; } -int ribmgr_cdap_read(struct cdap * instance, - int invoke_id, - char * name) -{ - LOG_MISSING; - (void) instance; - (void) invoke_id; - (void) name; - - return -1; -} - -int ribmgr_cdap_write(struct cdap * instance, - int invoke_id, - char * name, - uint8_t * data, - size_t len, - uint32_t flags) +static int ribmgr_cdap_write(struct cdap * instance, + int invoke_id, + char * name, + uint8_t * data, + size_t len, + uint32_t flags) { static_info_msg_t * msg; int ret = 0; @@ -290,41 +278,9 @@ int ribmgr_cdap_write(struct cdap * instance, return 0; } -int ribmgr_cdap_create(struct cdap * instance, - int invoke_id, - char * name, - uint8_t * data, - size_t len) -{ - LOG_MISSING; - (void) instance; - (void) invoke_id; - (void) name; - (void) data; - (void) len; - - return -1; -} - -int ribmgr_cdap_delete(struct cdap * instance, - int invoke_id, - char * name, - uint8_t * data, - size_t len) -{ - LOG_MISSING; - (void) instance; - (void) invoke_id; - (void) name; - (void) data; - (void) len; - - return -1; -} - -int ribmgr_cdap_start(struct cdap * instance, - int invoke_id, - char * name) +static int ribmgr_cdap_start(struct cdap * instance, + int invoke_id, + char * name) { static_info_msg_t stat_info = STATIC_INFO_MSG__INIT; uint8_t * data = NULL; @@ -372,7 +328,8 @@ int ribmgr_cdap_start(struct cdap * instance, pthread_mutex_lock(&rib.cdap_reqs_lock); - iid = cdap_send_write(instance, STATIC_INFO, data, len, 0); + iid = cdap_send_request(instance, CDAP_WRITE, + STATIC_INFO, data, len, 0); if (iid < 0) { pthread_mutex_unlock(&rib.cdap_reqs_lock); pthread_rwlock_unlock(&ipcpi.state_lock); @@ -381,7 +338,8 @@ int ribmgr_cdap_start(struct cdap * instance, return -1; } - if (cdap_result_wait(instance, WRITE, STATIC_INFO, iid)) { + if (cdap_result_wait(instance, CDAP_WRITE, + STATIC_INFO, iid)) { pthread_mutex_unlock(&rib.cdap_reqs_lock); pthread_rwlock_unlock(&ipcpi.state_lock); free(data); @@ -396,7 +354,8 @@ int ribmgr_cdap_start(struct cdap * instance, pthread_mutex_lock(&rib.cdap_reqs_lock); - iid = cdap_send_stop(instance, ENROLLMENT); + iid = cdap_send_request(instance, CDAP_STOP, ENROLLMENT, + NULL, 0, 0); if (iid < 0) { pthread_mutex_unlock(&rib.cdap_reqs_lock); pthread_rwlock_unlock(&ipcpi.state_lock); @@ -405,7 +364,8 @@ int ribmgr_cdap_start(struct cdap * instance, return -1; } - if (cdap_result_wait(instance, STOP, ENROLLMENT, iid)) { + if (cdap_result_wait(instance, CDAP_STOP, + ENROLLMENT, iid)) { pthread_mutex_unlock(&rib.cdap_reqs_lock); pthread_rwlock_unlock(&ipcpi.state_lock); free(data); @@ -427,9 +387,9 @@ int ribmgr_cdap_start(struct cdap * instance, return 0; } -int ribmgr_cdap_stop(struct cdap * instance, - int invoke_id, - char * name) +static int ribmgr_cdap_stop(struct cdap * instance, + int invoke_id, + char * name) { int ret = 0; @@ -452,14 +412,37 @@ int ribmgr_cdap_stop(struct cdap * instance, return 0; } +static int ribmgr_cdap_request(struct cdap * instance, + int invoke_id, + enum cdap_opcode opcode, + char * name, + uint8_t * data, + size_t len, + uint32_t flags) +{ + switch (opcode) { + case CDAP_WRITE: + return ribmgr_cdap_write(instance, + invoke_id, + name, data, + len, flags); + case CDAP_START: + return ribmgr_cdap_start(instance, + invoke_id, + name); + case CDAP_STOP: + return ribmgr_cdap_stop(instance, + invoke_id, + name); + default: + LOG_INFO("Unsupported CDAP opcode received."); + return -1; + } +} + static struct cdap_ops ribmgr_ops = { - .cdap_reply = ribmgr_cdap_reply, - .cdap_read = ribmgr_cdap_read, - .cdap_write = ribmgr_cdap_write, - .cdap_create = ribmgr_cdap_create, - .cdap_delete = ribmgr_cdap_delete, - .cdap_start = ribmgr_cdap_start, - .cdap_stop = ribmgr_cdap_stop + .cdap_reply = ribmgr_cdap_reply, + .cdap_request = ribmgr_cdap_request }; int ribmgr_add_flow(int fd) @@ -489,8 +472,10 @@ int ribmgr_add_flow(int fd) ipcp_set_state(IPCP_PENDING_ENROLL); pthread_mutex_lock(&rib.cdap_reqs_lock); - iid = cdap_send_start(instance, - ENROLLMENT); + iid = cdap_send_request(instance, + CDAP_START, + ENROLLMENT, + NULL, 0, 0); if (iid < 0) { pthread_mutex_unlock(&rib.cdap_reqs_lock); pthread_rwlock_unlock(&rib.flows_lock); @@ -501,7 +486,8 @@ int ribmgr_add_flow(int fd) return -1; } - if (cdap_result_wait(instance, START, ENROLLMENT, iid)) { + if (cdap_result_wait(instance, CDAP_START, + ENROLLMENT, iid)) { pthread_mutex_unlock(&rib.cdap_reqs_lock); pthread_rwlock_unlock(&rib.flows_lock); pthread_rwlock_unlock(&ipcpi.state_lock); diff --git a/src/lib/cdap.c b/src/lib/cdap.c index 4a6408f6..d06a7d39 100644 --- a/src/lib/cdap.c +++ b/src/lib/cdap.c @@ -83,49 +83,58 @@ static void * handle_cdap_msg(void * o) switch (msg->opcode) { case OPCODE__READ: if (msg->name != NULL) - instance->ops->cdap_read(instance, - msg->invoke_id, - msg->name); + instance->ops->cdap_request(instance, + msg->invoke_id, + CDAP_READ, + msg->name, + NULL, 0, 0); break; case OPCODE__WRITE: if (msg->name != NULL && msg->has_value) - instance->ops->cdap_write(instance, - msg->invoke_id, - msg->name, - msg->value.data, - msg->value.len, - msg->flags); + instance->ops->cdap_request(instance, + msg->invoke_id, + CDAP_WRITE, + msg->name, + msg->value.data, + msg->value.len, + msg->flags); break; case OPCODE__CREATE: if (msg->name != NULL && msg->has_value) - instance->ops->cdap_create(instance, - msg->invoke_id, - msg->name, - msg->value.data, - msg->value.len); + instance->ops->cdap_request(instance, + msg->invoke_id, + CDAP_CREATE, + msg->name, + msg->value.data, + msg->value.len, 0); break; case OPCODE__DELETE: if (msg->name != NULL && msg->has_value) - instance->ops->cdap_delete(instance, - msg->invoke_id, - msg->name, - msg->value.data, - msg->value.len); + instance->ops->cdap_request(instance, + msg->invoke_id, + CDAP_DELETE, + msg->name, + msg->value.data, + msg->value.len, 0); break; case OPCODE__START: if (msg->name != NULL) - instance->ops->cdap_start(instance, - msg->invoke_id, - msg->name); + instance->ops->cdap_request(instance, + msg->invoke_id, + CDAP_START, + msg->name, + NULL, 0, 0); break; case OPCODE__STOP: if (msg->name != NULL) - instance->ops->cdap_stop(instance, - msg->invoke_id, - msg->name); + instance->ops->cdap_request(instance, + msg->invoke_id, + CDAP_STOP, + msg->name, + NULL, 0, 0); break; case OPCODE__REPLY: instance->ops->cdap_reply(instance, @@ -191,12 +200,7 @@ struct cdap * cdap_create(struct cdap_ops * ops, if (ops == NULL || fd < 0 || ops->cdap_reply == NULL || - ops->cdap_read == NULL || - ops->cdap_write == NULL || - ops->cdap_create == NULL || - ops->cdap_delete == NULL || - ops->cdap_start == NULL || - ops->cdap_stop == NULL) + ops->cdap_request == NULL) return NULL; flags = flow_cntl(fd, FLOW_F_GETFL, 0); @@ -274,9 +278,12 @@ static int write_msg(struct cdap * instance, return ret; } -static int send_read_or_start_or_stop(struct cdap * instance, - char * name, - opcode_t code) +int cdap_send_request(struct cdap * instance, + enum cdap_opcode code, + char * name, + uint8_t * data, + size_t len, + uint32_t flags) { int id; cdap_t msg = CDAP__INIT; @@ -288,75 +295,39 @@ static int send_read_or_start_or_stop(struct cdap * instance, if (!bmp_is_id_valid(instance->ids, id)) return -1; - msg.opcode = code; - msg.invoke_id = id; - msg.name = name; - - if (write_msg(instance, &msg)) - return -1; - - return id; -} - -static int send_create_or_delete(struct cdap * instance, - char * name, - uint8_t * data, - size_t len, - opcode_t code) -{ - int id; - cdap_t msg = CDAP__INIT; - - if (instance == NULL || name == NULL || data == NULL) - return -1; - - id = next_invoke_id(instance); - if (!bmp_is_id_valid(instance->ids, id)) - return -1; - - msg.opcode = code; - msg.name = name; - msg.invoke_id = id; - msg.has_value = true; - msg.value.data = data; - msg.value.len = len; - - if (write_msg(instance, &msg)) - return -1; - - return id; -} - -int cdap_send_read(struct cdap * instance, - char * name) -{ - return send_read_or_start_or_stop(instance, name, OPCODE__READ); -} - -int cdap_send_write(struct cdap * instance, - char * name, - uint8_t * data, - size_t len, - uint32_t flags) -{ - int id; - cdap_t msg = CDAP__INIT; - - if (instance == NULL || name == NULL || data == NULL) - return -1; - - id = next_invoke_id(instance); - if (!bmp_is_id_valid(instance->ids, id)) + switch (code) { + case CDAP_READ: + msg.opcode = OPCODE__READ; + break; + case CDAP_WRITE: + msg.opcode = OPCODE__WRITE; + break; + case CDAP_CREATE: + msg.opcode = OPCODE__CREATE; + break; + case CDAP_DELETE: + msg.opcode = OPCODE__DELETE; + break; + case CDAP_START: + msg.opcode = OPCODE__START; + break; + case CDAP_STOP: + msg.opcode = OPCODE__STOP; + break; + default: + release_invoke_id(instance, id); return -1; + } - msg.opcode = OPCODE__WRITE; msg.name = name; msg.has_flags = true; msg.flags = flags; msg.invoke_id = id; - msg.has_value = true; - msg.value.data = data; - msg.value.len = len; + if (data != NULL) { + msg.has_value = true; + msg.value.data = data; + msg.value.len = len; + } if (write_msg(instance, &msg)) return -1; @@ -364,34 +335,6 @@ int cdap_send_write(struct cdap * instance, return id; } -int cdap_send_create(struct cdap * instance, - char * name, - uint8_t * data, - size_t len) -{ - return send_create_or_delete(instance, name, data, len, OPCODE__CREATE); -} - -int cdap_send_delete(struct cdap * instance, - char * name, - uint8_t * data, - size_t len) -{ - return send_create_or_delete(instance, name, data, len, OPCODE__DELETE); -} - -int cdap_send_start(struct cdap * instance, - char * name) -{ - return send_read_or_start_or_stop(instance, name, OPCODE__START); -} - -int cdap_send_stop(struct cdap * instance, - char * name) -{ - return send_read_or_start_or_stop(instance, name, OPCODE__STOP); -} - int cdap_send_reply(struct cdap * instance, int invoke_id, int result, -- cgit v1.2.3