Browse Source

...

generate_api
Volker Birk 4 years ago
parent
commit
c9b8b3c33f
10 changed files with 223 additions and 225 deletions
  1. +124
    -0
      src/Sync_helper.c
  2. +45
    -0
      src/Sync_helper.h
  3. +0
    -40
      src/asn1_helper.c
  4. +0
    -16
      src/asn1_helper.h
  5. +2
    -2
      src/pEp_internal.h
  6. +12
    -97
      src/sync.c
  7. +17
    -48
      src/sync.h
  8. +6
    -6
      src/sync_impl.c
  9. +4
    -4
      src/sync_impl.h
  10. +13
    -12
      sync/gen_statemachine.ysl2

+ 124
- 0
src/Sync_helper.c View File

@ -0,0 +1,124 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "Sync_helper.h"
#include "pEp_internal.h"
typedef struct growing_buf {
char *data;
size_t size;
} growing_buf_t;
growing_buf_t *new_growing_buf(void)
{
growing_buf_t *result = calloc(1, sizeof(growing_buf_t));
assert(result);
return result;
}
void free_growing_buf(growing_buf_t *buf)
{
if (buf) {
free(buf->data);
free(buf);
}
}
int consume_bytes(const void *src, size_t size, growing_buf_t *dst)
{
assert(src && dst);
if (!(src && dst))
return -1;
char *new_data = realloc(dst->data, dst->size + size + 1);
assert(new_data);
if (!new_data)
return -1;
dst->data = new_data;
memcpy(dst->data + dst->size, src, size);
dst->size += size;
dst->data[dst->size] = 0; // safeguard
return 1;
}
DYNAMIC_API PEP_STATUS decode_sync_msg(
const char *data,
size_t size,
char **text
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(data && text);
if (!(data && text))
return PEP_ILLEGAL_VALUE;
*text = NULL;
Sync_t *msg = NULL;
uper_decode_complete(NULL, &asn_DEF_Sync, (void **) &msg, data, size);
if (!msg)
return PEP_SYNC_ILLEGAL_MESSAGE;
growing_buf_t *dst = new_growing_buf();
if (!dst) {
status = PEP_OUT_OF_MEMORY;
goto the_end;
}
asn_enc_rval_t er = xer_encode(&asn_DEF_Sync, msg, XER_F_BASIC,
(asn_app_consume_bytes_f *) consume_bytes, (void *) dst);
if (er.encoded == -1) {
status = PEP_CANNOT_ENCODE;
goto the_end;
}
*text = dst->data;
dst->data = NULL;
the_end:
free_growing_buf(dst);
ASN_STRUCT_FREE(asn_DEF_Sync, msg);
return status;
}
DYNAMIC_API PEP_STATUS encode_sync_msg(
const char *text,
char **data,
size_t *size
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(text && data && size);
if (!(text && data && size))
return PEP_ILLEGAL_VALUE;
*data = NULL;
*size = 0;
Sync_t *msg = NULL;
asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_Sync, (void **) &msg,
(const void *) text, strlen(text));
if (dr.code != RC_OK) {
status = PEP_SYNC_ILLEGAL_MESSAGE;
goto the_end;
}
char *payload = NULL;
ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_Sync, NULL, msg,
(void **) &payload);
if (_size == -1) {
status = PEP_CANNOT_ENCODE;
goto the_end;
}
*data = payload;
*size = (size_t) _size;
the_end:
ASN_STRUCT_FREE(asn_DEF_Sync, msg);
return status;
}

+ 45
- 0
src/Sync_helper.h View File

@ -0,0 +1,45 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#pragma once
#include "pEpEngine.h"
#ifdef __cplusplus
extern "C" {
#endif
// decode_sync_msg() - decode sync message from PER into XER
//
// parameters:
// data (in) PER encoded data
// size (in) size of PER encoded data
// text (out) XER text of the same sync message
DYNAMIC_API PEP_STATUS decode_sync_msg(
const char *data,
size_t size,
char **text
);
// encode_sync_msg() - encode sync message from XER into PER
//
// parameters:
// text (in) string with XER text of the sync message
// data (out) PER encoded data
// size (out) size of PER encoded data
DYNAMIC_API PEP_STATUS encode_sync_msg(
const char *text,
char **data,
size_t *size
);
#ifdef __cplusplus
}
#endif

+ 0
- 40
src/asn1_helper.c View File

@ -1,40 +0,0 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "asn1_helper.h"
#include <assert.h>
#include <stdlib.h>
growing_buf_t *new_growing_buf(void)
{
growing_buf_t *result = calloc(1, sizeof(growing_buf_t));
assert(result);
return result;
}
void free_growing_buf(growing_buf_t *buf)
{
if (buf) {
free(buf->data);
free(buf);
}
}
int consume_bytes(const void *src, size_t size, growing_buf_t *dst)
{
assert(src && dst);
if (!(src && dst))
return -1;
char *new_data = realloc(dst->data, dst->size + size + 1);
assert(new_data);
if (!new_data)
return -1;
dst->data = new_data;
memcpy(dst->data + dst->size, src, size);
dst->size += size;
dst->data[dst->size] = 0; // safeguard
return 1;
}

+ 0
- 16
src/asn1_helper.h View File

@ -1,16 +0,0 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#pragma once
#include <string.h>
typedef struct growing_buf {
char *data;
size_t size;
} growing_buf_t;
growing_buf_t *new_growing_buf(void);
void free_growing_buf(growing_buf_t *buf);
int consume_bytes(const void *src, size_t size, growing_buf_t *dst);

+ 2
- 2
src/pEp_internal.h View File

@ -205,8 +205,8 @@ struct _pEpSession {
void *sync_management;
void *sync_obj;
notifyHandshake_t notifyHandshake;
inject_sync_msg_t inject_sync_msg;
retrieve_next_sync_msg_t retrieve_next_sync_msg;
inject_sync_event_t inject_sync_event;
retrieve_next_sync_event_t retrieve_next_sync_event;
// pEp Sync
struct Sync_state_s sync_state;


+ 12
- 97
src/sync.c View File

@ -6,30 +6,26 @@
#include <memory.h>
#include <assert.h>
#include "asn1_helper.h"
#include "../asn.1/Sync.h"
#include "KeySync_fsm.h"
// receive_sync_msg is defined in the sync_impl
DYNAMIC_API PEP_STATUS register_sync_callbacks(
PEP_SESSION session,
void *management,
notifyHandshake_t notifyHandshake,
inject_sync_msg_t inject_sync_msg,
retrieve_next_sync_msg_t retrieve_next_sync_msg
inject_sync_event_t inject_sync_event,
retrieve_next_sync_event_t retrieve_next_sync_event
)
{
assert(session && management && notifyHandshake && inject_sync_msg && retrieve_next_sync_msg);
if (!(session && management && notifyHandshake && inject_sync_msg && retrieve_next_sync_msg))
assert(session && management && notifyHandshake && inject_sync_event && retrieve_next_sync_event);
if (!(session && management && notifyHandshake && inject_sync_event && retrieve_next_sync_event))
return PEP_ILLEGAL_VALUE;
session->sync_management = management;
session->notifyHandshake = notifyHandshake;
session->inject_sync_msg = inject_sync_msg;
session->retrieve_next_sync_msg = retrieve_next_sync_msg;
session->inject_sync_event = inject_sync_event;
session->retrieve_next_sync_event = retrieve_next_sync_event;
return status;
return PEP_STATUS_OK;
}
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
@ -39,8 +35,8 @@ DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
// unregister
session->sync_management = NULL;
session->notifyHandshake = NULL;
session->inject_sync_msg = NULL;
session->retrieve_next_sync_msg = NULL;
session->inject_sync_event = NULL;
session->retrieve_next_sync_event = NULL;
}
DYNAMIC_API PEP_STATUS deliverHandshakeResult(
@ -94,10 +90,9 @@ DYNAMIC_API PEP_STATUS do_sync_protocol(
{
Sync_t *msg = NULL;
PEP_STATUS status = PEP_STATUS_OK;
time_t timeout = 0;
assert(session && session->retrieve_next_sync_msg);
if (!(session && session->retrieve_next_sync_msg))
assert(session && session->retrieve_next_sync_event);
if (!(session && session->retrieve_next_sync_event))
return PEP_ILLEGAL_VALUE;
log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
@ -105,7 +100,7 @@ DYNAMIC_API PEP_STATUS do_sync_protocol(
session->sync_obj = obj;
while (true)
{
msg = session->retrieve_next_sync_msg(session->sync_management, &timeout);
event = session->retrieve_next_sync_event(session->sync_management);
if (msg == NULL)
break;
@ -124,83 +119,3 @@ DYNAMIC_API PEP_STATUS do_sync_protocol(
return PEP_STATUS_OK;
}
DYNAMIC_API PEP_STATUS decode_sync_msg(
const char *data,
size_t size,
char **text
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(data && text);
if (!(data && text))
return PEP_ILLEGAL_VALUE;
*text = NULL;
Sync_t *msg = NULL;
uper_decode_complete(NULL, &asn_DEF_Sync, (void **) &msg, data, size);
if (!msg)
return PEP_SYNC_ILLEGAL_MESSAGE;
growing_buf_t *dst = new_growing_buf();
if (!dst) {
status = PEP_OUT_OF_MEMORY;
goto the_end;
}
asn_enc_rval_t er = xer_encode(&asn_DEF_Sync, msg, XER_F_BASIC,
(asn_app_consume_bytes_f *) consume_bytes, (void *) dst);
if (er.encoded == -1) {
status = PEP_CANNOT_ENCODE;
goto the_end;
}
*text = dst->data;
dst->data = NULL;
the_end:
free_growing_buf(dst);
ASN_STRUCT_FREE(asn_DEF_Sync, msg);
return status;
}
DYNAMIC_API PEP_STATUS encode_sync_msg(
const char *text,
char **data,
size_t *size
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(text && data && size);
if (!(text && data && size))
return PEP_ILLEGAL_VALUE;
*data = NULL;
*size = 0;
Sync_t *msg = NULL;
asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_Sync, (void **) &msg,
(const void *) text, strlen(text));
if (dr.code != RC_OK) {
status = PEP_SYNC_ILLEGAL_MESSAGE;
goto the_end;
}
char *payload = NULL;
ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_Sync, NULL, msg,
(void **) &payload);
if (_size == -1) {
status = PEP_CANNOT_ENCODE;
goto the_end;
}
*data = payload;
*size = (size_t) _size;
the_end:
ASN_STRUCT_FREE(asn_DEF_Sync, msg);
return status;
}

+ 17
- 48
src/sync.h View File

@ -3,16 +3,16 @@
#pragma once
#include "message.h"
#include "Sync_event.h"
#ifdef __cplusplus
extern "C" {
#endif
struct Sync;
typedef enum _sync_handshake_signal {
SYNC_NOTIFY_UNDEFINED = 0,
@ -75,40 +75,36 @@ DYNAMIC_API PEP_STATUS deliverHandshakeResult(
);
// inject_sync_msg - inject sync protocol message
// inject_sync_event - inject sync protocol message
//
// parameters:
// msg (in) message to inject
// ev (in) event to inject
// management (in) application defined
//
// return value:
// 0 if msg could be stored successfully or nonzero otherwise
// 0 if event could be stored successfully or nonzero otherwise
typedef int (*inject_sync_msg_t)(struct Sync *msg, void *management);
typedef int (*inject_sync_event_t)(Sync_event_t *ev, void *management);
// retrieve_next_sync_msg - receive next sync message
// retrieve_next_sync_event - receive next sync event
//
// parameters:
// management (in) application defined
// timeout (in,out) do not wait longer than timeout for message
// timeout == NULL or *timeout == 0 is blocking
//
// return value:
// next message, then timeout[out] == remaining time
// NULL and timeout[out] != 0 for timeout occurence
// NULL and timeout[out] == 0 for termination
// next event
typedef struct Sync *(*retrieve_next_sync_msg_t)(void *management, time_t *timeout);
typedef struct Sync_event_t *(*retrieve_next_sync_event_t)(void *management);
// register_sync_callbacks() - register adapter's callbacks
//
// parameters:
// session (in) session where to store obj handle
// management (in) application defined
// notifyHandshake (in) callback for doing the handshake
// retrieve_next_sync_msg (in) callback for receiving sync messages
// session (in) session where to store obj handle
// management (in) application defined
// notifyHandshake (in) callback for doing the handshake
// retrieve_next_sync_event (in) callback for receiving sync event
//
// return value:
// PEP_STATUS_OK or any other value on errror
@ -120,12 +116,13 @@ DYNAMIC_API PEP_STATUS register_sync_callbacks(
PEP_SESSION session,
void *management,
notifyHandshake_t notifyHandshake,
inject_sync_msg_t inject_sync_msg,
retrieve_next_sync_msg_t retrieve_next_sync_msg
inject_sync_event_t inject_sync_event,
retrieve_next_sync_event_t retrieve_next_sync_event
);
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
// do_sync_protocol() - function to be run on an extra thread
//
// parameters:
@ -149,34 +146,6 @@ DYNAMIC_API PEP_STATUS do_sync_protocol(
);
// decode_sync_msg() - decode sync message from PER into XER
//
// parameters:
// data (in) PER encoded data
// size (in) size of PER encoded data
// text (out) XER text of the same sync message
DYNAMIC_API PEP_STATUS decode_sync_msg(
const char *data,
size_t size,
char **text
);
// encode_sync_msg() - encode sync message from XER into PER
//
// parameters:
// text (in) string with XER text of the sync message
// data (out) PER encoded data
// size (out) size of PER encoded data
DYNAMIC_API PEP_STATUS encode_sync_msg(
const char *text,
char **data,
size_t *size
);
#ifdef __cplusplus
}
#endif


+ 6
- 6
src/sync_impl.c View File

@ -3,6 +3,7 @@
#include "Sync_impl.h"
#include "pEp_internal.h"
#include "Sync_event.h"
#include "KeySync_fsm.h"
PEP_STATUS Sync_driver(
@ -19,7 +20,8 @@ PEP_STATUS Sync_driver(
do {
switch (fsm) {
case Sync_PR_keysync: {
next_state = fsm_KeySync(session, session->sync_state.keysync.state, event);
int state = session->sync_state.keysync.state;
next_state = fsm_KeySync(session, state, event);
if (next_state > None) {
session->sync_state.keysync.state = next_state;
event = Init;
@ -53,14 +55,13 @@ PEP_STATUS inject_Sync_event(
PEP_STATUS status = PEP_STATUS_OK;
if (!session->inject_sync_msg) {
if (!session->inject_sync_event) {
status = PEP_SYNC_NO_INJECT_CALLBACK;
goto error;
}
if (event < Extra) {
msg = new_Sync_message(fsm, event);
assert(msg);
if (!msg) {
status = PEP_OUT_OF_MEMORY;
goto error;
@ -82,7 +83,7 @@ PEP_STATUS inject_Sync_event(
ev->event = event;
ev->msg = msg;
int result = session->inject_sync_msg(ev,
int result = session->inject_sync_event(ev,
session->sync_management);
if (result) {
status = PEP_STATEMACHINE_ERROR;
@ -112,7 +113,6 @@ PEP_STATUS Sync_notify(
PEP_STATUS status = PEP_STATUS_OK;
Sync_t *msg = new_Sync_message(fsm, message_type);
assert(msg);
if (!msg) {
status = PEP_OUT_OF_MEMORY;
goto error;
@ -133,7 +133,7 @@ the_end:
PEP_STATUS recv_Sync_event(
PEP_SESSION session,
Sync_t *ev
Sync_event_t *ev
)
{
assert(session && ev);


+ 4
- 4
src/sync_impl.h View File

@ -5,7 +5,7 @@
#include "fsm_common.h"
#include "message_api.h"
#include "../asn.1/Sync.h"
#include "Sync_event.h"
#ifdef __cplusplus
extern "C" {
@ -41,7 +41,7 @@ PEP_STATUS Sync_notify(
int message_type
);
// send message about an event to partners using state
// send message about an event to communication partners using state
PEP_STATUS send_Sync_message(
PEP_SESSION session,
@ -51,9 +51,9 @@ PEP_STATUS send_Sync_message(
// receive message and store it in state
PEP_STATUS recv_Sync_message(
PEP_STATUS recv_Sync_event(
PEP_SESSION session,
Sync_t *msg
Sync_event_t *ev
);
// state machine driver


+ 13
- 12
sync/gen_statemachine.ysl2 View File

@ -76,6 +76,7 @@ tstylesheet {
#include "pEp_internal.h"
#include "«@name»_event.h"
#include "«@name»_func.h"
`` for "fsm" | #include "«@name»_fsm.h"
DYNAMIC_API «@name»_event_t *new_«@name»_event(«@name»_PR fsm, int event, «@name»_t *msg)
{
@ -124,7 +125,7 @@ tstylesheet {
#include "fsm_common.h"
#include "message_api.h"
#include "../asn.1/Sync.h"
#include "«@name»_event.h"
#ifdef __cplusplus
extern "C" {
@ -154,7 +155,7 @@ tstylesheet {
int message_type
);
// send message about an event to partners using state
// send message about an event to communication partners using state
PEP_STATUS send_«@name»_message(
PEP_SESSION session,
@ -164,9 +165,9 @@ tstylesheet {
// receive message and store it in state
PEP_STATUS recv_«@name»_message(
PEP_STATUS recv_«@name»_event(
PEP_SESSION session,
«@name»_t *msg
«@name»_event_t *ev
);
// state machine driver
@ -199,6 +200,7 @@ tstylesheet {
#include "«@name»_impl.h"
#include "pEp_internal.h"
#include "«@name»_event.h"
`` for "fsm" | #include "«@name»_fsm.h"
PEP_STATUS «@name»_driver(
@ -238,14 +240,13 @@ tstylesheet {
PEP_STATUS status = PEP_STATUS_OK;
if (!session->inject_«yml:lcase(@name)»_msg) {
if (!session->inject_«yml:lcase(@name)»_event) {
status = PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
goto error;
}
if (event < Extra) {
msg = new_«@name»_message(fsm, event);
assert(msg);
if (!msg) {
status = PEP_OUT_OF_MEMORY;
goto error;
@ -267,7 +268,7 @@ tstylesheet {
ev->event = event;
ev->msg = msg;
int result = session->inject_«yml:lcase(@name)»_msg(ev,
int result = session->inject_«yml:lcase(@name)»_event(ev,
session->«yml:lcase(@name)»_management);
if (result) {
status = PEP_STATEMACHINE_ERROR;
@ -297,7 +298,6 @@ tstylesheet {
PEP_STATUS status = PEP_STATUS_OK;
«@name»_t *msg = new_«@name»_message(fsm, message_type);
assert(msg);
if (!msg) {
status = PEP_OUT_OF_MEMORY;
goto error;
@ -318,7 +318,7 @@ tstylesheet {
PEP_STATUS recv_«@name»_event(
PEP_SESSION session,
«@name»_t *ev
«@name»_event_t *ev
)
{
assert(session && ev);
@ -371,7 +371,7 @@ tstylesheet {
{
||
case Sync_PR_«yml:lcase(@name)»: {
switch (msg->choice.keysync.choice.present) {
switch (msg->choice.«yml:lcase(@name)».payload.present) {
||
for "message"
||
@ -394,7 +394,8 @@ tstylesheet {
template "fsm", mode=driver
||
case Sync_PR_«yml:lcase(@name)»: {
next_state = fsm_«@name»(session, session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state, event);
int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
next_state = fsm_«@name»(session, state, event);
if (next_state > None) {
session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = next_state;
event = Init;
@ -637,7 +638,7 @@ tstylesheet {
||
«$fsm/@name»_SERVICE_LOG("send message", "«$name»");
status = «$protocol/@name»_notify(session, «$fsm/@id», «$name»);
status = send_«$protocol/@name»_message(session, «$fsm/@id», «$name»);
||
}
otherwise


Loading…
Cancel
Save