00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023 #include <stdio.h>
00024 #include <stddef.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <sys/types.h>
00028 #include <sys/socket.h>
00029 #include <netinet/in.h>
00030 #include <arpa/inet.h>
00031
00032 #include "erdm.h"
00033 #include "erdminternal.h"
00034 #include "erdmlog.h"
00035 #include "erdmServer.h"
00036
00037 static int fInit = 0;
00038 static int sockfd = -1;
00039 static struct sockaddr_in serverAdr;
00040
00041
00042
00043
00044 int dmMessageInit(char *szDestination)
00045 {
00046 int port;
00047 int ret;
00048 struct in_addr destinationAdr;
00049
00050
00051
00052
00053
00054 if ((ret = inet_pton(AF_INET, szDestination, &destinationAdr)) == 1)
00055 {
00056 ERDM_LOGPRINTF("Sending messages to %s.\n", szDestination);
00057 }
00058 else if (ret == 0)
00059 {
00060 ERDM_ERRORPRINTF("%s not a presentation IP address (not in dotted decimal format).\n", szDestination);
00061 return -1;
00062 }
00063 else
00064 {
00065 ERDM_ERRORPRINTF("Failed to convert to a numeric IP address.\n");
00066 return -2;
00067 }
00068
00069 dmGetServerPort(&port);
00070
00071 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
00072 bzero(&serverAdr, sizeof(serverAdr));
00073 serverAdr.sin_family = AF_INET;
00074 serverAdr.sin_addr.s_addr = destinationAdr.s_addr;
00075 serverAdr.sin_port = htons(port);
00076
00077 fInit = 1;
00078 return 0;
00079 }
00080
00081 void dmMessageFramer(char *szMessage, uDmCommand * pDmCmd)
00082 {
00083 switch ((int) pDmCmd->dmCmdGeneric.cmd)
00084 {
00085 case (int) dmCcDisplay:
00086 sprintf(szMessage, "!%d,%d,%d", (int) pDmCmd->dmCmdGeneric.cmd, (int) pDmCmd->dmCmdDisplay.priority,
00087 (int) pDmCmd->dmCmdDisplay.qual);
00088 break;
00089
00090 case (int) dmCcDisplayPartial:
00091 sprintf(szMessage, "!%d,%d,%d,%d,%d,%d,%d", (int) pDmCmd->dmCmdGeneric.cmd, (int) pDmCmd->dmCmdDisplay.priority,
00092 (int) pDmCmd->dmCmdDisplay.qual, pDmCmd->dmCmdDisplayPartial.xUp, pDmCmd->dmCmdDisplayPartial.yUp,
00093 pDmCmd->dmCmdDisplayPartial.xDown, pDmCmd->dmCmdDisplayPartial.yDown);
00094 break;
00095
00096 case (int) dmCcEraseToWhite:
00097 sprintf(szMessage, "!%d,%d", (int) pDmCmd->dmCmdGeneric.cmd, (int) pDmCmd->dmCmdDisplay.priority);
00098 break;
00099
00100 default:
00101 strcpy(szMessage, "error");
00102 ERDM_ERRORPRINTF("Command Code %d not supported", (int) pDmCmd->dmCmdGeneric.cmd);
00103 break;
00104 }
00105 }
00106
00107 int dmDisplay(eDmCmdPriority prior, eDmQuality qual)
00108 {
00109 char szMsg[DM_MAXCHARONLINE];
00110 uDmCommand dmCmd;
00111
00112 dmCmd.dmCmdDisplay.cmd = dmCcDisplay;
00113 dmCmd.dmCmdDisplay.priority = prior;
00114 dmCmd.dmCmdDisplay.qual = qual;
00115
00116 dmMessageFramer(szMsg, &dmCmd);
00117
00118 if (!fInit)
00119 {
00120 dmMessageInit("127.0.0.1");
00121 }
00122
00123 sendto(sockfd, szMsg, strlen(szMsg), 0, (struct sockaddr *) &serverAdr, sizeof(serverAdr));
00124 return 0;
00125 }
00126
00127 int dmDisplayPartial(eDmCmdPriority prior, eDmQuality qual, int xUp, int yUp, int xDown, int yDown)
00128 {
00129 char szMsg[DM_MAXCHARONLINE];
00130 uDmCommand dmCmd;
00131
00132 dmCmd.dmCmdDisplayPartial.cmd = dmCcDisplayPartial;
00133 dmCmd.dmCmdDisplayPartial.priority = prior;
00134 dmCmd.dmCmdDisplayPartial.qual = qual;
00135 dmCmd.dmCmdDisplayPartial.xUp = xUp;
00136 dmCmd.dmCmdDisplayPartial.yUp = yUp;
00137 dmCmd.dmCmdDisplayPartial.xDown = xDown;
00138 dmCmd.dmCmdDisplayPartial.yDown = yDown;
00139
00140 dmMessageFramer(szMsg, &dmCmd);
00141
00142 if (!fInit)
00143 {
00144 dmMessageInit("127.0.0.1");
00145 }
00146
00147 sendto(sockfd, szMsg, strlen(szMsg), 0, (struct sockaddr *) &serverAdr, sizeof(serverAdr));
00148 return 0;
00149 }
00150
00151 int dmDisplayEraseToWhite(eDmCmdPriority prior)
00152 {
00153 char szMsg[DM_MAXCHARONLINE];
00154 uDmCommand dmCmd;
00155
00156 dmCmd.dmCmdDisplay.cmd = dmCcEraseToWhite;
00157 dmCmd.dmCmdDisplay.priority = prior;
00158
00159 dmMessageFramer(szMsg, &dmCmd);
00160
00161 if (!fInit)
00162 {
00163 dmMessageInit("127.0.0.1");
00164 }
00165
00166 sendto(sockfd, szMsg, strlen(szMsg), 0, (struct sockaddr *) &serverAdr, sizeof(serverAdr));
00167 return 0;
00168 }
00169
00170 int dmDump(eDmCmdPriority prior)
00171 {
00172 ERDM_ERRORPRINTF("dmDump is not implemented.\n");
00173 return -1;
00174 }