summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/p1905_managerd/src/alme.c
blob: 1c446c1fa4b8349e7db04c2700709aee1c7201fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * cleopatre/application/p1905_managerd/src/alme.c
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <assert.h>
#include <fcntl.h>

#include "p1905_managerd.h"
#include "cmdu_tlv.h"
#include "cmdu_message.h"
#include <sys/socket.h>
#include <sys/un.h>

#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/sem.h>

//#define ALME_DEBUG
#ifdef ALME_DEBUG
#define debug(...)          printf(__VA_ARGS__)
#define debug_syslog(...)        printf(__VA_ARGS__)
#else
#define debug(...)
#define debug_syslog(format, ...)   syslog(LOG_WARNING, format, ##__VA_ARGS__)
#endif

typedef union semun
{
  int val;
  struct semid_ds *buf;
  unsigned short *array;
}semun_t;

static void parse_alme_get_metric_req(struct p1905_managerd_ctx *ctx, ALME_MSG *msg)
{
    ALME_GET_METRIC_REQ *req;
#ifdef SUPPORT_WIFI
    unsigned char sta_itf[6];
#endif
    req = (ALME_GET_METRIC_REQ *)msg->body;
    //memcpy(ctx->link_metric_query_para.target, req->mac_addr, 6);
    memcpy(ctx->link_metric_query_para.target, ctx->p1905_al_mac_addr, 6);

    /*ALME does not define which type we should use, so I profile both RX/TX*/
    ctx->link_metric_query_para.type = BOTH_TX_AND_RX_METRICS;

    ctx->mid++;//message id increment
    /*record message id, need to compare with the mid value from link metric response*/
    ctx->link_metric_query_mid = ctx->mid;
    /*query the link metrics of neighbor, send link metric query*/

#ifdef SUPPORT_WIFI
    /* if a AP want to send unicast cmdu to STA, it needs to use interface mac
     * instead of AL mac address
     */
     if(is_neighbor_wifi_sta(ctx, sta_itf, req->mac_addr))
     {
         debug_syslog("3. STA neighbor: %.2x %.2x %.2x %.2x %.2x %.2x\n",
             sta_itf[0], sta_itf[1], sta_itf[2], sta_itf[3], sta_itf[4], sta_itf[5]);
         insert_cmdu_txq(sta_itf, ctx->p1905_al_mac_addr,
            e_link_metric_response, ctx->mid);
     }
     else
#endif
        insert_cmdu_txq(req->mac_addr, ctx->p1905_al_mac_addr,\
                              e_link_metric_query, ctx->mid);

    /*set alme state to alme_wait_4_link_metrics_rsp*/
    ctx->alme_state = alme_wait_4_link_metrics_rsp;
}

int send_alme_get_metric_rsp(int fd, ALME_MSG *msg, ALME_REASON_CODE code)
{
    int len;

    ALME_GET_METRIC_RSP *rsp;

    msg->header.type = alme_get_metric_rsp;
    msg->header.length = sizeof(ALME_GET_METRIC_RSP);

    rsp = (ALME_GET_METRIC_RSP *)msg->body;
    rsp->reason_code = code;

    len = send(fd, msg, sizeof(ALME_MSG), 0);
    if(len < 0)
    {
        debug_syslog("send ALME_GET_METRIC_RSP error(%s) reason %d\n",strerror(errno), code);
    }
    return len;
}

int wait_4_link_metric_rsp_timeout(int fd)
{
    ALME_MSG msg;
    int len;

    len = send_alme_get_metric_rsp(fd, &msg, alme_failure);
    return len;
}

int send_alme_get_intf_list_rsp(struct p1905_managerd_ctx *ctx)
{
    int len, i;
    ALME_MSG msg;
    ALME_GET_INTF_LIST_RSP *rsp;
    unsigned intf_num = 0;
    struct p1905_neighbor_info *dev_info;


    msg.header.type = alme_get_intf_list_rsp;
    msg.header.length = sizeof(ALME_GET_INTF_LIST_RSP);

    rsp = (ALME_GET_INTF_LIST_RSP *)msg.body;
    rsp->intfnum = 0;

    intf_num = ITF_NUM;

    if(ITF_NUM > MAX_1905_LINK_NUM)
    {
        debug_syslog("ALME_GET_INTF_LIST_RSP too mush interface, %d\n", ITF_NUM);
        intf_num = MAX_1905_LINK_NUM;
    }

    for(i=0;i<intf_num;i++)
    {
        memcpy(rsp->intflist[i].mac_addr, ctx->itf[i].mac_addr, 6);
        rsp->intflist[i].intf_type = ctx->itf[i].media_type;
        if(!LIST_EMPTY(&(ctx->p1905_neighbor_dev[i].p1905nbr_head)))
        {
            LIST_FOREACH(dev_info, &(ctx->p1905_neighbor_dev[i].p1905nbr_head),
                     p1905nbr_entry)
            {
                if(dev_info->ieee802_1_bridge == 1)
                {
                    rsp->intflist[i].ieee802_1_bridge = 1;
                    break;
                }
            }
        }
        else
            rsp->intflist[i].ieee802_1_bridge = 0;

        rsp->intfnum++;
    }

    len = send(ctx->almefd, &msg, sizeof(ALME_MSG), 0);
    if(len < 0)
    {
        debug_syslog("send ALME_GET_INTF_LIST_RSP error(%s)\n",strerror(errno));
    }
    return len;
}

int send_alme_get_1905_neighbor_rsp(struct p1905_managerd_ctx *ctx)
{
    int len;
    ALME_MSG msg;
    ALME_GET_1905_NEIGHBOR_RSP *rsp;
    struct topology_discovery_db *tpg_db;
    int i = 0;

    msg.header.type = alme_get_1905_neighbor_rsp;
    msg.header.length = sizeof(ALME_GET_1905_NEIGHBOR_RSP);

    rsp = (ALME_GET_1905_NEIGHBOR_RSP *)msg.body;
    rsp->neighbornum = 0;

    /*query the topology discovery database to get 1905.1 neighbor*/
    if(!LIST_EMPTY(&(ctx->topology_entry.tpddb_head)))
    {
        i = 0;
        LIST_FOREACH(tpg_db, &(ctx->topology_entry.tpddb_head), tpddb_entry)
        {
            if(rsp->neighbornum == MAX_1905_NEIGHBOR_NUM)
                break;

            memcpy(&(rsp->neighborlist[i*6]), tpg_db->al_mac, 6);
            rsp->neighbornum += 1;
            i++;
        }
    }

    len = send(ctx->almefd, &msg, sizeof(ALME_MSG), 0);
    if(len < 0)
    {
        debug_syslog("send ALME_GET_1905_NEIGHBOR_RSP error(%s)\n",strerror(errno));
    }
    return len;
}

int ALME_init(struct p1905_managerd_ctx *ctx)
{
    struct sockaddr_un sock_addr;
    int status = 0;

    ctx->sock_alme = socket(AF_UNIX, SOCK_STREAM , 0);
    if(ctx->sock_alme < 0)
    {
        debug_syslog("open ALME socket error\n");
        return -1;
    }

    memset(&sock_addr, 0, sizeof(sock_addr));
    sock_addr.sun_family = AF_UNIX;
    strcpy(sock_addr.sun_path, "/tmp/alme_server");
    status = bind(ctx->sock_alme, (struct sockaddr *)&sock_addr, sizeof(sock_addr));
    if(status < 0)
    {
        close(ctx->sock_alme);
        debug_syslog("bind ALME fail(%s)\n",strerror(errno));
        return -1;
    }

    if(listen(ctx->sock_alme, 1) < 0)
    {
        close(ctx->sock_alme);
        debug_syslog("listen ALME fail(%s)\n",strerror(errno));
        return -1;
    }

    ctx->alme_state = alme_none;
    ctx->almefd = -1;
    ctx->alme_wait_4_link_metrics_cnt = 0;

    return 0;
}

int ALME_sync_init(struct p1905_managerd_ctx *ctx)
{
    semun_t arg;
    key_t key;

    key = ftok("/tmp/alme_server",'a');
    if(key == -1)
    {
        debug_syslog("create key for semaphore fail(%s)\n",strerror(errno));
        close(ctx->sock_alme);
        return -1;
    }

    /*create synchroniztion mechanism for libalme use*/
    ctx->semid = semget(key, 1, 0666|IPC_CREAT);
    if(ctx->semid < 0)
    {
        debug_syslog("create semaphore fail(%s)\n",strerror(errno));
        close(ctx->sock_alme);
        return -1;
    }

    arg.val = 1;
    if (semctl(ctx->semid, 0, SETVAL, arg) == -1)
    {
        debug_syslog("init semaphore fail(%s)\n",strerror(errno));
        close(ctx->sock_alme);
 
        if(semctl(ctx->semid, 0, IPC_RMID, arg) == -1)
        {
             debug_syslog("de-allocate semaphore fail(%s)\n",strerror(errno));
             return -1;
        }
        return -1;
    }

    return 0;
}

int ALME_uninit(struct p1905_managerd_ctx *ctx)
{
    close(ctx->sock_alme);

    if(ctx->almefd > 0)
    {
        close(ctx->almefd);
        ctx->almefd = -1;
    }
}

int ALME_sync_uninit(struct p1905_managerd_ctx *ctx)
{
    semun_t arg;

    if(semctl(ctx->semid, 0, IPC_RMID, arg) == -1)
    {
        debug_syslog("de-allocate semaphore fail(%s)\n",strerror(errno));
        return -1;
    }
    return 0;
}

int alme_receive(struct p1905_managerd_ctx *ctx, unsigned char *buf, int len)
{
    struct sockaddr_un from;
    int lenfrom = sizeof(from);

    ctx->almefd = accept(ctx->sock_alme, (struct sockaddr *)&from, &lenfrom);
    if(ctx->almefd < 0)
    {
        debug_syslog("accept alme fail\n");
        return -1;
    }

    len = recv(ctx->almefd, buf, len, 0);
    if(0 > len)
    {
        debug_syslog("recv alme fail\n");

        if(ctx->almefd > 0)
        {
            close(ctx->almefd);
            ctx->almefd = -1;
        }
        return -1;
    }

    /* we don't close ctx->almefd here because we may need this file descriptor
     * to feedback alme message to HLE
     */
    //close(ctx->almefd);
    return len;
}

int alme_process(struct p1905_managerd_ctx *ctx, unsigned char *buf, int len)
{
    ALME_MSG *msg;

    msg = (ALME_MSG *)buf;

    switch(msg->header.type)
    {
        case alme_get_metric_req:
            parse_alme_get_metric_req(ctx, msg);
            break;
        case alme_get_intf_list_req:
            send_alme_get_intf_list_rsp(ctx);
            break;

        /*below is MStar private*/    
        case alme_get_1905_neighbor_req:
            send_alme_get_1905_neighbor_rsp(ctx);
            break;
        default:
            break;
    }


    /* for some alme message, we can free the server and client connection.
     * for alme_get_metric_req, we need to wait for receiving the link metrics response and then free connection.
     * for .........
     */
    if(msg->header.type != alme_get_metric_req)
    {
        if(ctx->almefd > 0)
        {
            close(ctx->almefd);
            ctx->almefd = -1;
        }
    }

    return 0;
}