﻿/*
 *  UDP クライアント
 *
 *  OS: T-Kernel
 *
 */

#include <tk/tkernel.h>  /* T-Kernel ヘッダ */
#include <tm/tmonitor.h> /* tm_printf() など */
#include <libstr.h>      /* memcpy() など */
#include <utkn/utkn.h>   /* 6LoWPAN */

/* オブジェクト ID 番号 */
LOCAL ID tidA;           /* タスク A の ID */

/* タスク A */
LOCAL void taskA(INT stacd, void *exinf)
{
    static const utkn_6ln_init_t param = {
        .channel = RF_CHANNEL_ANY,
        .scan_type = WPAN_SCAN_ACTIVE,
        .scan_duration = 4,
        .options = 0,
        .tmout = TMO_FEVR,
    };
    static const udp6_cep_t dst = {
        .addr = {{0x20, 0x01, 0x0d, 0xb8,
                  0x00, 0x00, 0x00, 0x00,
                  0xb0, 0xe7, 0x41, 0xb7,
                  0xb6, 0xc0, 0x36, 0xf7, /* UDP サーバの IPv6 アドレス */
            }},
        .port = htons(9000), /* UDP サーバのポート番号 */
    };
    static udp6_cep_t cep;
    ID cepid;
    INT len;
    static UB buf[16];

    /* 6LoWPAN プロトコルスタックを初期化する */
    utkn_6ln_init(&param);

    /* UDP 通信端点をオープンする */
    cep.addr = ip6_addr_unspec;
    cep.port = UDP6_PORT_ANY;
    cepid = udp6_open(&cep);

    for (;; tk_dly_tsk(1000)) {
        /* UDP 送信する */
        len = 5;
        memcpy(buf, "hello", len);
        udp6_send(cepid, buf, len, &dst, 5000);

        /* UDP 受信する */
        len = udp6_recv(cepid, buf, sizeof(buf), NULL, NULL, NULL, 5000);
        if (len < 0) {
            tm_printf((UB*)"time out\n");
            continue;
        }
        tm_printf((UB*)"%.*s\n", len, buf);
    }

    /* このタスクは終了しない */
}

/* 初期化処理 */
EXPORT INT usermain(void)
{
    T_CTSK ct;

    /* タスク A を生成する */
    ct.exinf = (void*)('t' | 's' << 8 | 'k' << 16 | 'A' << 24); /* 拡張情報 */
    ct.tskatr = TA_HLNG | TA_RNG0;  /* タスク属性 */
    ct.task = taskA;                /* タスク起動アドレス */
    ct.itskpri = 1;                 /* タスク起動時優先度 */
    ct.stksz = 1024;                /* スタックサイズ */
    tidA = tk_cre_tsk(&ct);         /* タスク A を生成 */
    if (tidA < E_OK) return tidA;   /* タスク生成失敗時 */

    tk_sta_tsk(tidA, 0);            /* タスク A を起動 */

    return E_OK;
}
