⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msg.c

📁 IEEE 1588-2002编程源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* msg.c *//* see spec annex d */#include "../ptpd.h"Boolean msgPeek(void *buf){  /* not imlpemented yet */  return TRUE;}void msgUnpackHeader(void *buf, MsgHeader *header){  header->versionPTP = flip16(*(UInteger16*)(buf + 0));  header->versionNetwork = flip16(*(UInteger16*)(buf + 2));  DBGV("msgUnpackHeader: versionPTP %d\n", header->versionPTP);  DBGV("msgUnpackHeader: versionNetwork %d\n", header->versionNetwork);    memcpy(header->subdomain, (buf + 4), 16);  DBGV("msgUnpackHeader: subdomain %s\n", header->subdomain);    header->messageType = *(UInteger8*)(buf + 20);  header->sourceCommunicationTechnology = *(UInteger8*)(buf + 21);  DBGV("msgUnpackHeader: messageType %d\n", header->messageType);  DBGV("msgUnpackHeader: sourceCommunicationTechnology %d\n", header->sourceCommunicationTechnology);    memcpy(header->sourceUuid, (buf + 22), 6);  DBGV("msgUnpackHeader: sourceUuid %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",    header->sourceUuid[0], header->sourceUuid[1], header->sourceUuid[2],    header->sourceUuid[3], header->sourceUuid[4], header->sourceUuid[5]);    header->sourcePortId = flip16(*(UInteger16*)(buf + 28));  header->sequenceId = flip16(*(UInteger16*)(buf + 30));  DBGV("msgUnpackHeader: sourcePortId %d\n", header->sourcePortId);  DBGV("msgUnpackHeader: sequenceId %d\n", header->sequenceId);    header->control = *(UInteger8*)(buf + 32);  DBGV("msgUnpackHeader: control %d\n", header->control);    memcpy(header->flags, (buf + 34), 2);  DBGV("msgUnpackHeader: flags %02hhx %02hhx\n", header->flags[0], header->flags[1]);}void msgUnpackSync(void *buf, MsgSync *sync){  sync->originTimestamp.seconds = flip32(*(UInteger32*)(buf + 40));  DBGV("msgUnpackSync: originTimestamp.seconds %lu\n", sync->originTimestamp.seconds);  sync->originTimestamp.nanoseconds = flip32(*(Integer32*)(buf + 44));  DBGV("msgUnpackSync: originTimestamp.nanoseconds %ld\n", sync->originTimestamp.nanoseconds);  sync->epochNumber = flip16(*(UInteger16*)(buf + 48));  DBGV("msgUnpackSync: epochNumber %d\n", sync->epochNumber);  sync->currentUTCOffset = flip16(*(Integer16*)(buf + 50));  DBGV("msgUnpackSync: currentUTCOffset %d\n", sync->currentUTCOffset);  sync->grandmasterCommunicationTechnology = *(UInteger8*)(buf + 53);  DBGV("msgUnpackSync: grandmasterCommunicationTechnology %d\n", sync->grandmasterCommunicationTechnology);  memcpy(sync->grandmasterClockUuid, (buf + 54), 6);  DBGV("msgUnpackSync: grandmasterClockUuid %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",    sync->grandmasterClockUuid[0], sync->grandmasterClockUuid[1], sync->grandmasterClockUuid[2],    sync->grandmasterClockUuid[3], sync->grandmasterClockUuid[4], sync->grandmasterClockUuid[5]);  sync->grandmasterPortId = flip16(*(UInteger16*)(buf + 60));  DBGV("msgUnpackSync: grandmasterPortId %d\n", sync->grandmasterPortId);  sync->grandmasterSequenceId = flip16(*(UInteger16*)(buf + 62));  DBGV("msgUnpackSync: grandmasterSequenceId %d\n", sync->grandmasterSequenceId);  sync->grandmasterClockStratum = *(UInteger8*)(buf + 67);  DBGV("msgUnpackSync: grandmasterClockStratum %d\n", sync->grandmasterClockStratum);  memcpy(sync->grandmasterClockIdentifier, (buf + 68), 4);  DBGV("msgUnpackSync: grandmasterClockIdentifier %c%c%c%c\n",    sync->grandmasterClockIdentifier[0], sync->grandmasterClockIdentifier[1],    sync->grandmasterClockIdentifier[2], sync->grandmasterClockIdentifier[3]);  sync->grandmasterClockVariance = flip16(*(Integer16*)(buf + 74));  DBGV("msgUnpackSync: grandmasterClockVariance %d\n", sync->grandmasterClockVariance);  sync->grandmasterPreferred = *(UInteger8*)(buf + 77);  DBGV("msgUnpackSync: grandmasterPreferred %d\n", sync->grandmasterPreferred);  sync->grandmasterIsBoundaryClock = *(UInteger8*)(buf + 79);  DBGV("msgUnpackSync: grandmasterIsBoundaryClock %d\n", sync->grandmasterIsBoundaryClock);  sync->syncInterval = *(Integer8*)(buf + 83);  DBGV("msgUnpackSync: syncInterval %d\n", sync->syncInterval);  sync->localClockVariance = flip16(*(Integer16*)(buf + 86));  DBGV("msgUnpackSync: localClockVariance %d\n", sync->localClockVariance);  sync->localStepsRemoved = flip16(*(UInteger16*)(buf + 90));  DBGV("msgUnpackSync: localStepsRemoved %d\n", sync->localStepsRemoved);  sync->localClockStratum = *(UInteger8*)(buf + 95);  DBGV("msgUnpackSync: localClockStratum %d\n", sync->localClockStratum);  memcpy(sync->localClockIdentifer, (buf + 96), PTP_CODE_STRING_LENGTH);  DBGV("msgUnpackSync: localClockIdentifer %c%c%c%c\n",     sync->localClockIdentifer[0], sync->localClockIdentifer[1],    sync->localClockIdentifer[2], sync->localClockIdentifer[3]);  sync->parentCommunicationTechnology = *(UInteger8*)(buf + 101);  DBGV("msgUnpackSync: parentCommunicationTechnology %d\n", sync->parentCommunicationTechnology);  memcpy(sync->parentUuid, (buf + 102), PTP_UUID_LENGTH);  DBGV("msgUnpackSync: parentUuid %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",    sync->parentUuid[0], sync->parentUuid[1], sync->parentUuid[2],    sync->parentUuid[3], sync->parentUuid[4], sync->parentUuid[5]);  sync->parentPortField = flip16(*(UInteger16*)(buf + 110));  DBGV("msgUnpackSync: parentPortField %d\n", sync->parentPortField);  sync->estimatedMasterVariance = flip16(*(Integer16*)(buf + 114));  DBGV("msgUnpackSync: estimatedMasterVariance %d\n", sync->estimatedMasterVariance);  sync->estimatedMasterDrift = flip32(*(Integer32*)(buf + 116));  DBGV("msgUnpackSync: estimatedMasterDrift %ld\n", sync->estimatedMasterDrift);  sync->utcReasonable = *(UInteger8*)(buf + 123);  DBGV("msgUnpackSync: utcReasonable %d\n", sync->utcReasonable);}void msgUnpackDelayReq(void *buf, MsgDelayReq *req) {}void msgUnpackFollowUp(void *buf, MsgFollowUp *follow){  follow->associatedSequenceId = flip16(*(UInteger16*)(buf + 42));  DBGV("msgUnpackFollowUp: associatedSequenceId %u\n", follow->associatedSequenceId);  follow->preciseOriginTimestamp.seconds = flip32(*(UInteger32*)(buf + 44));  DBGV("msgUnpackFollowUp: preciseOriginTimestamp.seconds %lu\n", follow->preciseOriginTimestamp.seconds);  follow->preciseOriginTimestamp.nanoseconds = flip32(*(Integer32*)(buf + 48));  DBGV("msgUnpackFollowUp: preciseOriginTimestamp.nanoseconds %ld\n", follow->preciseOriginTimestamp.nanoseconds);}void msgUnpackDelayResp(void *buf, MsgDelayResp *resp){  resp->delayReceiptTimestamp.seconds = flip32(*(UInteger32*)(buf + 40));  DBGV("msgUnpackDelayResp: delayReceiptTimestamp.seconds %lu\n", resp->delayReceiptTimestamp.seconds);  resp->delayReceiptTimestamp.nanoseconds = flip32(*(Integer32*)(buf + 44));  DBGV("msgUnpackDelayResp: delayReceiptTimestamp.nanoseconds %ld\n", resp->delayReceiptTimestamp.nanoseconds);  resp->requestingSourceCommunicationTechnology = *(UInteger8*)(buf + 49);  DBGV("msgUnpackDelayResp: requestingSourceCommunicationTechnology %d\n", resp->requestingSourceCommunicationTechnology);  memcpy(resp->requestingSourceUuid, (buf + 50), 6);  DBGV("msgUnpackDelayResp: requestingSourceUuid %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",    resp->requestingSourceUuid[0], resp->requestingSourceUuid[1], resp->requestingSourceUuid[2],    resp->requestingSourceUuid[3], resp->requestingSourceUuid[4], resp->requestingSourceUuid[5]);  resp->requestingSourcePortId = flip16(*(UInteger16*)(buf + 56));  DBGV("msgUnpackDelayResp: requestingSourcePortId %d\n", resp->requestingSourcePortId);  resp->requestingSourceSequenceId = flip16(*(UInteger16*)(buf + 58));  DBGV("msgUnpackDelayResp: requestingSourceSequenceId %d\n", resp->requestingSourceSequenceId);}void msgUnpackManagement(void *buf, MsgManagement *manage){  manage->targetCommunicationTechnology = *(UInteger8*)(buf + 41);  memcpy(manage->targetUuid, (buf + 42), 6);  manage->targetPortId = flip16(*(UInteger16*)(buf + 48));  manage->startingBoundaryHops = flip16(*(Integer16*)(buf + 50));  manage->boundaryHops = flip16(*(Integer16*)(buf + 52));  manage->managementMessageKey = *(UInteger8*)(buf + 55);  manage->parameterLength = flip16(*(UInteger16*)(buf + 58));    if(manage->managementMessageKey == PTP_MM_GET_FOREIGN_DATA_SET)    manage->recordKey = flip16(*(UInteger16*)(buf + 62));}UInteger8 msgUnloadManagement(void *buf, MsgManagement *manage,  PtpClock *ptpClock, RunTimeOpts *rtOpts){  switch(manage->managementMessageKey)  {  case PTP_MM_INITIALIZE_CLOCK:    if(ptpClock->initializable)      return PTP_INITIALIZING;    break;      case PTP_MM_GOTO_FAULTY_STATE:    DBG("event FAULT_DETECTED (forced by management message)\n");    return PTP_FAULTY;    break;      case PTP_MM_DISABLE_PORT:    if(manage->targetPortId == 1)    {      DBG("event DESIGNATED_DISABLED\n");      return PTP_DISABLED;    }    break;      case PTP_MM_ENABLE_PORT:    if(manage->targetPortId == 1)    {      DBG("event DESIGNATED_ENABLED\n");      return PTP_INITIALIZING;    }    break;      case PTP_MM_CLEAR_DESIGNATED_PREFERRED_MASTER:    ptpClock->preferred = FALSE;    break;        case PTP_MM_SET_DESIGNATED_PREFERRED_MASTER:    ptpClock->preferred = TRUE;    break;      case PTP_MM_DISABLE_BURST:    break;      case PTP_MM_ENABLE_BURST:    break;      case PTP_MM_SET_SYNC_INTERVAL:    rtOpts->syncInterval = *(Integer8*)(buf + 63);    break;      case PTP_MM_SET_SUBDOMAIN:    memcpy(rtOpts->subdomainName, buf + 60, 16);    DBG("set subdomain to %s\n", rtOpts->subdomainName);    break;      case PTP_MM_SET_TIME:    *(Integer32*)buf = flip32(*(UInteger32*)(buf + 60));    *(Integer32*)(buf + 4) = flip32(*(Integer32*)(buf + 64));    setTime(*(UInteger32*)(buf + 60), *(Integer32*)(buf + 64));    break;      case PTP_MM_UPDATE_DEFAULT_DATA_SET:    if(!rtOpts->slaveOnly)      rtOpts->clockStratum = *(UInteger8*)(buf + 63);    memcpy(rtOpts->clockIdentifier, buf + 64, 4);    rtOpts->clockVariance = flip16(*(Integer16*)(buf + 70));    rtOpts->clockPreferred = *(UInteger8*)(buf + 75);    rtOpts->syncInterval = *(UInteger8*)(buf + 79);    memcpy(rtOpts->subdomainName, buf + 80, 16);    break;      case PTP_MM_UPDATE_GLOBAL_TIME_PROPERTIES:    ptpClock->current_utc_offset = flip16(*(Integer16*)(buf + 62));    ptpClock->leap_59 = *(UInteger8*)(buf + 67);    ptpClock->leap_61 = *(UInteger8*)(buf + 71);    ptpClock->epoch_number = flip16(*(UInteger16*)(buf + 74));    break;      default:    break;  }    return ptpClock->port_state;}void msgPackHeader(void *buf, PtpClock *ptpClock){  *(Integer32*)(buf + 0) = shift16(flip16(VERSION_PTP), 0) | shift16(flip16(VERSION_NETWORK), 1);  memcpy((buf + 4), ptpClock->subdomain_name, 16);  *(Integer32*)(buf + 20) = shift8(ptpClock->port_communication_technology, 1);  memcpy((buf + 22), ptpClock->port_uuid_field, 6);    if(ptpClock->external_timing)    setFlag((buf + 34), PTP_EXT_SYNC);  if(ptpClock->clock_followup_capable)    setFlag((buf + 34), PTP_ASSIST);  if(ptpClock->is_boundary_clock)    setFlag((buf + 34), PTP_BOUNDARY_CLOCK);}void msgPackSync(void *buf, Boolean burst, PtpClock *ptpClock)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -