You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于nesC的7节点路由表实现报错求助:unary *类型参数无效

问题:nesC路由表访问语法错误及代码修正

路由表定义

// Define routing table structure
typedef nx_struct routing_table_t {
    nx_uint16_t destination;
    nx_uint16_t next_hop;
    nx_uint16_t cost;
} routing_table_t;

routing_table_t routing_table[7]; // 对应7个节点的网络

编译错误

RadioRouteC.nc: In function `Receive.receive':
RadioRouteC.nc:208: invalid type argument of `unary *'
RadioRouteC.nc:238: invalid type argument of `unary *'
RadioRouteC.nc:240: invalid type argument of `unary *'
RadioRouteC.nc:241: invalid type argument of `unary *'
RadioRouteC.nc:242: invalid type argument of `unary *'
RadioRouteC.nc:264: invalid type argument of `unary *'

现有代码

Receive.receive事件实现

event message_t* Receive.receive(message_t* bufPtr, 
                   void* payload, uint8_t len) {
    /*
    * THIS EVENT IMPLEMENTS FOLLOWING FUNCTIONALITIES:
    * Parse the receive packet.
    * Perform the packet send using the generate_send function 
    * Implement the LED logic and print LED status on Debug
    */

    
    // Update LED status
    int code = 10310011; // take my person code
    digit = code % 10; // take the digit
    led_index = digit % 3; // perform the modulo division if the digit
        if (led_index == 0) { // if index equals 0, toggle only LED 0 (same for 1 and 2 accordingly)
            call Leds.led0Toggle();
        } else if (led_index == 1) {
            call Leds.led1Toggle();
        } else if (led_index == 2) {
            call Leds.led2Toggle();
        }
    dbg("LED", "LED%d is toggled.\n", led_index); // notify about LED status change
    code = code / 10; // update the value of the person code (becomes 1 digit shorter for the next msg receipt)
    
  
// Parse the received packet
    switch (bufPtr->type) 
    { // implement the fuctionality switch based on message type that can be 0,1 or 2
        case 0: // Data message
            // Check if the node is the destination
            if (bufPtr->destination == TOS_NODE_ID) {
                // If the node is the destination, print the received data
                dbg("Data", "Received data %d from %d\n", bufPtr->value, bufPtr->sender);
            } else {
                // If the node is not the destination, forward the data - to do so, check the routing table
                // Check if the node has an entry in the routing table
                if (routing_table[bufPtr->destination].destination != 0) {
                    // If the node has an entry in the routing table, forward the data
                    bufPtr->sender = TOS_NODE_ID;
                    generate_send(routing_table[bufPtr->destination]->next_hop, bufPtr, 0); // ERROR LINE 208
                }
            }
            break;
        case 1: // Route Request message
            // Check if the node is the requested node
            if (bufPtr->node_requested == TOS_NODE_ID) {
                // If the node is the requested node, send a Route Reply message
                message_t reply;
                reply.type = 2;
                reply.sender = TOS_NODE_ID;
                reply.node_requested = bufPtr->node_requested;
                reply.cost = 1;
                generate_send(AM_BROADCAST_ADDR, &reply, 2);
            } else {
                // If the node is not the requested node, broadcast the Route Request message
                // Check if the node has already sent a Route Request message
                if (route_req_sent == FALSE) {
                    // If the node has not sent a Route Request message, broadcast the Route Request message
                    generate_send(AM_BROADCAST_ADDR, bufPtr, bufPtr->type);
                    route_req_sent = TRUE;
                }
            }
            break;
        case 2: // Route Reply message
            // Check if the node is the requested node
            if (bufPtr->node_requested == TOS_NODE_ID) {
                // If the node is the requested node, do nothing
            } else {
                // If the node is not the requested node, check if the node has an entry in the routing table
                if (routing_table[bufPtr->node_requested].destination == 0 || routing_table[bufPtr->node_requested]->cost > bufPtr->cost) // ERROR LINE 238
{
                    // If the node does not have an entry in the routing table or the new cost is lower than the current cost, update the routing table
                    routing_table[bufPtr->node_requested]->destination = bufPtr->node_requested; // ERROR LINE 240
                    routing_table[bufPtr->node_requested]->next_hop = bufPtr->sender; // ERROR LINE 241
                    routing_table[bufPtr->node_requested]->cost = bufPtr->cost + 1; // ERROR LINE 242
                    // Forward the Route Reply message
                    bufPtr->cost += 1;
                    generate_send(AM_BROADCAST_ADDR, bufPtr, bufPtr->type);
                }
            }
            break;
        default:
            // Handle any unexpected message types
            break;
        }
    
    // Check if the node is node 1
    if (TOS_NODE_ID == 1) {
        // If the node is node 1, check if the node has an entry in the routing table
            if (routing_table[1].destination == 7) { // checks if node 1 has an entry for node 7
                // If the node has an entry in the routing table, send a Data message
                message_t data;
                data.type = 0; // message type = data message
                data.sender = TOS_NODE_ID; // data sender is the current node (1)
                data.destination = 7; // message is destined to node 7
                data.value = 5; // message value is 5
                generate_send(routing_table[7]->next_hop, &data, 0); // ERROR LINE 264
            }
                 else {
                 // If the node does not have an entry in the routing table, send a Route Request message
                message_t req;
                req.type = 1;
                req.node_requested = 7;
                generate_send(AM_BROADCAST_ADDR, &req, 1);
            }

        
    return bufPtr;}

    }

Timer1.fired事件实现

event void Timer1.fired() {
    /*
    * Implement here the logic to trigger the Node 1 to send the first REQ packet
    */
    message_t packet;
    packet.type = 1;
    packet.node_requested = 7;
    generate_send(TOS_BCAST_ADDR, &packet, 1); // 1 for route req message
}

message_t结构定义

// Define message structure
typedef nx_struct message_t {
    // Define mesage type
    nx_uint8_t type; // 0 for data msg, 1 for route request, 2 for route reply
    
    // Data messages
    nx_uint16_t destination;
    nx_uint16_t value;

    // Route Reply messages
    nx_uint16_t sender;
    nx_uint16_t node_requested; // Route Request msg
    nx_uint16_t cost;
} message_t;

功能需求

  1. Timer1.fired()触发时,节点1发送类型为1、目标节点为7的广播路由请求包;
  2. Receive.receive()触发时解析数据包:
    • 若为数据报文(类型0),判断是否为目标节点,是则打印数据,否则查路由表转发;
    • 若为路由请求报文(类型1),判断是否为请求节点,是则回复路由响应,否则未发送过则广播转发;
    • 若为路由响应报文(类型2),判断是否为请求节点,否则查路由表,无条目或新成本更低则更新并转发;
    • 节点1需根据路由表状态发送数据报文或路由请求报文。

修正方案

1. 核心语法错误修复(unary *错误)

routing_table是结构体数组,数组元素为直接的结构体实例,而非指针。访问结构体成员需用.运算符,而非->(仅用于指针类型结构体)。所有报错行的->需替换为.,同时修正索引越界问题:

错误示例:

generate_send(routing_table[bufPtr->destination]->next_hop, bufPtr, 0);

修正后:

generate_send(routing_table[bufPtr->destination - 1].next_hop, bufPtr, 0);

2. 路由表索引越界修复

节点ID为1-7,但routing_table[7]的索引范围是0-6,直接用节点ID作为索引会导致越界。所有访问路由表的位置,需用节点ID - 1作为索引(或改为routing_table[8],索引0留空)。

3. LED逻辑修复

code为局部变量,每次进入receive事件都会重置,导致LED触发逻辑无效。需将code声明为全局变量:

// 文件顶部全局声明
static int code = 10310011;
static uint8_t digit, led_index;

4. 未定义变量修复

route_req_sent未声明,需全局声明:

static bool route_req_sent = FALSE;

5. 节点1路由表检查逻辑修复

原代码错误检查索引1的条目(对应节点2),需改为检查节点7对应的索引:

if (routing_table[7-1].destination == 7) {

完整修正后的Receive.receive代码

// 全局变量声明(文件顶部,事件函数外部)
static int code = 10310011;
static uint8_t digit, led_index;
static bool route_req_sent = FALSE;
routing_table_t routing_table[7]; // 索引0-6对应节点1-7

event message_t* Receive.receive(message_t* bufPtr, 
                   void* payload, uint8_t len) {
    // Update LED status
    digit = code % 10;
    led_index = digit % 3;
    if (led_index == 0) {
        call Leds.led0Toggle();
    } else if (led_index == 1) {
        call Leds.led1Toggle();
    } else if (led_index == 2) {
        call Leds.led2Toggle();
    }
    dbg("LED", "LED%d is toggled.\n", led_index);
    code = code / 10;
    
    // Parse the received packet
    switch (bufPtr->type) 
    {
        case 0: // Data message
            if (bufPtr->destination == TOS_NODE_ID) {
                dbg("Data", "Received data %d from %d\n", bufPtr->value, bufPtr->sender);
            } else {
                uint8_t rt_idx = bufPtr->destination - 1;
                if (rt_idx < 7 && routing_table[rt_idx].destination != 0) {
                    bufPtr->sender = TOS_NODE_ID;
                    generate_send(routing_table[rt_idx].next_hop, bufPtr, 0);
                }
            }
            break;
        case 1: // Route Request message
            if (bufPtr->node_requested == TOS_NODE_ID) {
                message_t reply;
                reply.type = 2;
                reply.sender = TOS_NODE_ID;
                reply.node_requested = bufPtr->node_requested;
                reply.cost = 1;
                generate_send(AM_BROADCAST_ADDR, &reply, 2);
            } else {
                if (!route_req_sent) {
                    generate_send(AM_BROADCAST_ADDR, bufPtr, bufPtr->type);
                    route_req_sent = TRUE;
                }
            }
            break;
        case 2: // Route Reply message
            if (bufPtr->node_requested != TOS_NODE_ID) {
                uint8_t rt_idx = bufPtr->node_requested - 1;
                if (rt_idx <7 && (routing_table[rt_idx].destination == 0 || routing_table[rt_idx].cost > bufPtr->cost)) {
                    routing_table[rt_idx].destination = bufPtr->node_requested;
                    routing_table[rt_idx].next_hop = bufPtr->sender;
                    routing_table[rt_idx].cost = bufPtr->cost + 1;
                    bufPtr->cost += 1;
                    generate_send(AM_BROADCAST_ADDR, bufPtr, bufPtr->type);
                }
            }
            break;
        default:
            break;
        }
    
    // Node 1 logic
    if (TOS_NODE_ID == 1) {
        uint8_t rt_idx = 7 -1; // 节点7对应索引6
        if (routing_table[rt_idx].destination == 7) {
            message_t data;
            data.type = 0;
            data.sender = TOS_NODE_ID;
            data.destination = 7;
            data.value = 5;
            generate_send(routing_table[rt_idx].next_hop, &data, 0);
        } else {
            message_t req;
            req.type = 1;
            req.node_requested = 7;
            generate_send(AM_BROADCAST_ADDR, &req, 1);
        }
    }

    return bufPtr;
}

其他小修正

  • Timer1.fired()中TOS_BCAST_ADDR替换为AM_BROADCAST_ADDR,保持一致性;
  • message_t结构中注释拼写错误:mesage改为message。

内容的提问来源于stack exchange,提问作者aleksa_but

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 02:47:07