Ping pong app demo.

Dependencies:   modem_ref_helper DebouncedInterrupt

main.cpp

Committer:
Jeej
Date:
2017-05-18
Revision:
2:785b422c7d22
Parent:
1:4629ccf8315d
Child:
3:0979d8cba5ec

File content as of revision 2:785b422c7d22:

// @autor: jeremie@wizzilab.com
// @date: 2017-05-02

#include "DebouncedInterrupt.h"
#include "modem_ref_helper.h"
#include "modem_callbacks.h"

Semaphore button_user(0);
Semaphore modem_ready[MAX_USER_NB];

enum {
    MODEM_RESP_NO,
    MODEM_RESP_TERMINAL,
    MODEM_RESP_DONE,
};

uint8_t g_main_id;
uint8_t g_void_id;

typedef struct {
    Thread* thread;
    uint32_t count;
    d7a_addressee_t addressee;
} ping_t;


#define MY_POLICY_IDX           0
#define FID_PING_PONG           128
#define PING_DELAY              500
#define PING_COUNTER_SIZE       sizeof(uint32_t)
#define MAX_PING_NB             MAX_USER_NB-2

d7a_xcl_t ping_pong_xcl = { .bf.s = 13, .bf.m = 1 };

alp_retry_policy_t my_policy = {
    .meta.procedure     = 0,
    .meta.respond       = true,
    .meta.persistant    = false,
    .meta.bulk          = false,
    .depth              = 1,
    .retries            = 0,
    .slot_time          = 0
};

alp_d7a_itf_t my_itf = {
    .type                           = ALP_ITF_TYPE_D7A,
    .cfg.to                         = 0,
    .cfg.te                         = 0,
    .cfg.qos.bf.resp                = D7A_RESP_NO,
    .cfg.qos.bf.retry               = MY_POLICY_IDX,
    .cfg.qos.bf.record              = 0,
    .cfg.qos.bf.stop_on_err         = 0,
    .cfg.addressee.ctrl.bf.nls      = D7A_NLS_AES_CCM_64,
    .cfg.addressee.ctrl.bf.idf      = D7A_ID_NOID,
    .cfg.addressee.xcl              = ping_pong_xcl
};

void print_status(int status)
{
    switch (status)
    {
        case ALP_ERR_NONE:
            PRINT("Status: OK\n");
            break;
        case ALP_ERR_FILE_EXIST:
            PRINT("Status: Already registered\n");
            break;
        default:
            PRINT("Status: error %d\n", status);
            break;
    }
}

void print_resp(int status)
{
    switch (status)
    {
        case ALP_ERR_NONE:
            PRINT("Resp: OK\n");
            break;
        case ALP_ERR_FILE_EXIST:
            PRINT("Resp: Already registered\n");
            break;
        default:
            PRINT("Resp: error %d\n", status);
            break;
    }
}

// Callback for User
void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
{
    (void)id;
    
    if (terminal)
    {    
        print_status(err);
        modem_ready[id].release();
    }
    else
    {
        print_resp(err);
    }
}

void my_void_callback(uint8_t terminal, int8_t err, uint8_t id)
{
    (void)terminal;
    (void)id;
    print_status(err);
}


// Interrupt Service Routine on button press.
void button_push_isr( void )
{
    button_user.release();
}

#ifdef DEBUG_LED
DigitalOut my_led(DEBUG_LED);
#endif

void my_udata(u8 fid,void *data,u32 offset,u32 length, u8 i_type, u8 i_length, u8* i_data)
{
    (void)data;
    (void)i_length;

    if (i_type == ALP_ITF_TYPE_D7A && fid == FID_PING_PONG && offset == 0 && length == PING_COUNTER_SIZE)
    {
        d7a_sp_res_t* istat = (d7a_sp_res_t*)i_data;
        uint32_t* count = (uint32_t*)data;
        
        PRINT("Got PING %d", *count);
        PRINT_DATA(" from ", "%02X", istat->addressee.id, 8, "");
        PRINT(" (rxlev:%d lb:%d)\n", istat->rxlev, istat->lb);
        
#ifdef DEBUG_LED
        my_led = 1;
#endif
    
        Thread::wait(PING_DELAY);
        
#ifdef DEBUG_LED
            my_led = 0;
#endif
        
        alp_d7a_itf_t resp_itf = {
            .type                           = ALP_ITF_TYPE_D7A,
            .cfg.to                         = 0,
            .cfg.te                         = 0,
            .cfg.qos.bf.resp                = D7A_RESP_NO,
            .cfg.qos.bf.retry               = MY_POLICY_IDX,
            .cfg.qos.bf.record              = 0,
            .cfg.qos.bf.stop_on_err         = 0,
            .cfg.addressee                  = istat->addressee,
        };
        
        (*count)++;
        
        // Send ping
        PRINT("Send PING %d", *count);
        PRINT_DATA(" to ", "%02X", resp_itf.cfg.addressee.id, 8, "\n");
        
        modem_send_file_content((uint8_t*)&resp_itf, D7_ITF_SIZE(&resp_itf), istat, FID_PING_PONG, count, 0, PING_COUNTER_SIZE, g_void_id);
    }
}

void button_user_thread()
{
    d7a_sp_res_t istat;
    uint32_t ping = 0;
        
    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
        
    while (true)
    {
        // Wait for button press
        button_user.wait();

        // Initiate ping
        PRINT("Initiate PING\n");
        modem_send_file_content((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), (void*)&istat, FID_PING_PONG, &ping, 0, PING_COUNTER_SIZE, g_main_id);
        modem_ready[g_main_id].wait();
    }
}

modem_callbacks_t callbacks = {
    .read       = my_read,
    .write      = my_write,
    .read_fprop = my_read_fprop,
    .flush      = my_flush,
    .remove     = my_delete,
    .udata      = my_udata,
    .lqual      = my_lqual,
    .ldown      = my_ldown,
    .reset      = my_reset,
    .boot       = my_boot
};

/*** Main function ------------------------------------------------------------- ***/
int main()
{
    // Start & initialize
    #ifdef DEBUG_LED
    DBG_OPEN(DEBUG_LED);
#else
    DBG_OPEN(NC);
#endif
    PRINT("\r\n--- Starting new run ---\r\n");
    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
    
    modem_helper_open(&callbacks);
    
    g_main_id = modem_get_id(my_main_callback);
    
    // Put modem to listen to this access class
    modem_write_file(D7A_FID_DLL_CFG, &ping_pong_xcl, 0, sizeof(d7a_xcl_t), g_main_id);
    modem_ready[g_main_id].wait();
    
    // Set custom retry policy
    modem_write_file(WM_FID_ALP_CFG, &my_policy, MY_POLICY_IDX * sizeof(alp_retry_policy_t), sizeof(alp_retry_policy_t), g_main_id);
    modem_ready[g_main_id].wait();
    
    // Configure URC: LQUAL on report file notification every 10 reports
    PRINT("Setup URCs\n");
    modem_enable_urc(ALP_URC_TYPE_LQUAL, IFID_REPORT, 10, true, g_main_id);
    modem_ready[g_main_id].wait();
    
    PRINT("Start D7A Stack\n");
    modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS, true, g_main_id);
    modem_ready[g_main_id].wait();
    
#ifdef DEBUG_BUTTON
    DebouncedInterrupt user_interrupt(DEBUG_BUTTON);
    user_interrupt.attach(button_push_isr, IRQ_FALL, 500, true);
    
    Thread but_th(osPriorityNormal, 1024, NULL);
    osStatus status = but_th.start(button_user_thread);
    ASSERT(status == osOK, "Failed to start but thread (err: %d)\r\n", status);
#endif
    
    // Set main task to lowest priority
    osThreadSetPriority(osThreadGetId(), osPriorityIdle);
    while(true)
    {
        Thread::wait(500);
    }
}