message_t

本文详细介绍了TinyOS操作系统中消息传递的结构定义,包括消息头、消息体、消息尾及元数据等部分,并阐述了这些组件如何共同作用于无线传感器网络的通信过程中。


//message.h

#include "platform_message.h"


#ifndef TOSH_DATA_LENGTH
#define TOSH_DATA_LENGTH 28
#endif


#ifndef TOS_BCAST_ADDR
#define TOS_BCAST_ADDR 0xFFFF
#endif


typedef nx_struct message_t {
  nx_uint8_t header[sizeof(message_header_t)];
  nx_uint8_t data[TOSH_DATA_LENGTH];
  nx_uint8_t footer[sizeof(message_footer_t)];
  nx_uint8_t metadata[sizeof(message_metadata_t)];
} message_t;


/*
 * This resource is used to arbitrate access between ActiveMessageC,
 * Ieee154MessageC and possibly future MessageC components to the 
 * underlying radio driver.
 */
#define RADIO_SEND_RESOURCE "RADIO_SEND_RESOURCE"



//platform_message.h

typedef union message_header {
  cc2420_header_t cc2420;
  serial_header_t serial;
} message_header_t;


typedef union TOSRadioFooter {
  cc2420_footer_t cc2420;
} message_footer_t;


typedef union TOSRadioMetadata {
  cc2420_metadata_t cc2420;
  serial_metadata_t serial;
} message_metadata_t;


//CC2420.h

typedef uint8_t cc2420_status_t;


#if defined(TFRAMES_ENABLED) && defined(IEEE154FRAMES_ENABLED)
#error "Both TFRAMES and IEEE154FRAMES enabled!"
#endif


/**
 * CC2420 header definition.
 * 
 * An I-frame (interoperability frame) header has an extra network 
 * byte specified by 6LowPAN
 * 
 * Length = length of the header + payload of the packet, minus the size
 *   of the length byte itself (1).  This is what allows for variable 
 *   length packets.
 * 
 * FCF = Frame Control Field, defined in the 802.15.4 specs and the
 *   CC2420 datasheet.
 *
 * DSN = Data Sequence Number, a number incremented for each packet sent
 *   by a particular node.  This is used in acknowledging that packet, 
 *   and also filtering out duplicate packets.
 *
 * DestPan = The destination PAN (personal area network) ID, so your 
 *   network can sit side by side with another TinyOS network and not
 *   interfere.
 * 
 * Dest = The destination address of this packet. 0xFFFF is the broadcast
 *   address.
 *
 * Src = The local node ID that generated the message.
 * 
 * Network = The TinyOS network ID, for interoperability with other types
 *   of 802.15.4 networks. 
 * 
 * Type = TinyOS AM type.  When you create a new AMSenderC(AM_MYMSG), 
 *   the AM_MYMSG definition is the type of packet.
 * 
 * TOSH_DATA_LENGTH defaults to 28, it represents the maximum size of 
 * the payload portion of the packet, and is specified in the 
 * tos/types/message.h file.
 *
 * All of these fields will be filled in automatically by the radio stack 
 * when you attempt to send a message.
 */
/**
 * CC2420 Security Header
 */
typedef nx_struct security_header_t {
  nx_uint8_t secLevel:3;
  nx_uint8_t keyMode:2;
  nx_uint8_t reserved:3;
  nx_uint32_t frameCounter;
  nx_uint8_t keyID[1]; // One byte for now
} security_header_t;


typedef nx_struct cc2420_header_t {
  nxle_uint8_t length;
  nxle_uint16_t fcf;
  nxle_uint8_t dsn;
  nxle_uint16_t destpan;
  nxle_uint16_t dest;
  nxle_uint16_t src;
  /** CC2420 802.15.4 header ends here */
#ifdef CC2420_HW_SECURITY
  security_header_t secHdr;
#endif
  
#ifndef TFRAMES_ENABLED
  /** I-Frame 6LowPAN interoperability byte */
  nxle_uint8_t network;
#endif


  nxle_uint8_t type;
} cc2420_header_t;


/**
 * CC2420 Packet Footer
 */
typedef nx_struct cc2420_footer_t {
} cc2420_footer_t;


/**
 * CC2420 Packet metadata. Contains extra information about the message
 * that will not be transmitted.
 *
 * Note that the first two bytes automatically take in the values of the
 * FCS when the payload is full. Do not modify the first two bytes of metadata.
 */
typedef nx_struct cc2420_metadata_t {
  nx_uint8_t rssi;
  nx_uint8_t lqi;
  nx_uint8_t tx_power;
  nx_bool crc;
  nx_bool ack;
  nx_bool timesync;
  nx_uint32_t timestamp;
  nx_uint16_t rxInterval;


  /** Packet Link Metadata */
#ifdef PACKET_LINK
  nx_uint16_t maxRetries;
  nx_uint16_t retryDelay;
#endif
} cc2420_metadata_t;




typedef nx_struct cc2420_packet_t {
  cc2420_header_t packet;
  nx_uint8_t data[];
} cc2420_packet_t;



### `phTmlNfc_DeferredCall` 函数调用及 `phDal4Nfc` 消息队列代码段含义和功能分析 #### 代码段分析 代码段 `phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId, (phLibNfc_Message_t*)&msg);` 调用了 `phTmlNfc_DeferredCall` 函数。该函数接收两个参数: - `gpphTmlNfc_Context->dwCallbackThreadId`:这可能是一个线程 ID,指定了要将消息发送到的目标线程。 - `(phLibNfc_Message_t*)&msg`:将 `msg` 转换为 `phLibNfc_Message_t` 类型的指针,作为要发送的消息。 #### 功能推测 - **延迟调用功能**:`phTmlNfc_DeferredCall` 函数名中的 “DeferredCall” 表明这可能是一个延迟调用函数,用于将消息发送到指定线程进行处理。这种延迟调用机制可以用于异步处理消息,避免在当前线程中阻塞执行。 - **消息传递**:该函数可能是 `phDal4Nfc` 消息队列系统的一部分,用于将消息从一个线程传递到另一个线程。通过将消息发送到指定线程的消息队列中,目标线程可以在合适的时机处理这些消息。 #### 可能存在的问题 - **线程安全问题**:如果多个线程同时调用 `phTmlNfc_DeferredCall` 函数,可能会导致消息队列的竞争条件。需要确保在多线程环境下对消息队列的访问是线程安全的。 - **消息丢失问题**:如果目标线程的消息队列已满,或者目标线程没有及时处理消息,可能会导致消息丢失。需要考虑如何处理消息队列满的情况,以及如何确保消息的可靠传递。 - **错误处理问题**:`phTmlNfc_DeferredCall` 函数可能会返回错误码,调用者需要检查这些错误码并进行相应的错误处理。 ### 示例代码 以下是一个简单的示例代码,展示了 `phTmlNfc_DeferredCall` 函数的可能实现: ```c #include <pthread.h> #include <queue> #include <mutex> // 定义消息类型 typedef struct { // 消息内容 int data; } phLibNfc_Message_t; // 定义消息队列 std::queue<phLibNfc_Message_t> message_queue; std::mutex queue_mutex; // 定义延迟调用函数 void phTmlNfc_DeferredCall(int thread_id, phLibNfc_Message_t* msg) { // 加锁以确保线程安全 std::lock_guard<std::mutex> lock(queue_mutex); // 将消息添加到消息队列中 message_queue.push(*msg); // 这里可以添加代码,通知目标线程有新消息到达 // 例如,使用条件变量或信号量 } // 目标线程处理消息的函数 void* target_thread(void* arg) { while (true) { // 加锁以确保线程安全 std::lock_guard<std::mutex> lock(queue_mutex); // 检查消息队列是否有消息 if (!message_queue.empty()) { // 取出消息 phLibNfc_Message_t msg = message_queue.front(); message_queue.pop(); // 处理消息 // ... } // 解锁 queue_mutex.unlock(); // 线程可以休眠一段时间,避免忙等待 // ... } return nullptr; } int main() { // 创建目标线程 pthread_t target_tid; pthread_create(&target_tid, nullptr, target_thread, nullptr); // 创建消息 phLibNfc_Message_t msg; msg.data = 123; // 调用延迟调用函数 phTmlNfc_DeferredCall(1, &msg); // 等待目标线程结束 pthread_join(target_tid, nullptr); return 0; } ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值