//----------------------------------------------------------------------- // Send state message to all active connections // return TRUE if message was send to at least one client //----------------------------------------------------------------------- BOOL Ros_StateServer_SendMsgToAllClient(Controller* controller, SimpleMsg* sendMsg, int msgSize) { int index; int ret; BOOL bHasConnections = FALSE; // Check all active connections for(index = 0; index < MAX_STATE_CONNECTIONS; index++) { if(controller->sdStateConnections[index] != INVALID_SOCKET) { ret = mpSend(controller->sdStateConnections[index], (char*)(sendMsg), msgSize, 0); if(ret <= 0) { printf("StateServer Send failure. Closing state server connection.\r\n"); mpClose(controller->sdStateConnections[index]); controller->sdStateConnections[index] = INVALID_SOCKET; } else { bHasConnections = TRUE; } } } return bHasConnections; }
int ehMpd(MW_EVENT event, int argi, void* argp) { switch (event) { case MW_INIT: if (mpConsoleMutex) return 0; // already inited memset(&mpx,0,sizeof(mpx)); MutexCreate(&mpConsoleMutex); if (loopclip) ThreadCreate(&mpThreadHandle, mpThread, 0); break; case MW_UNINIT: MutexDestroy(&mpConsoleMutex); mpClose(); break; case MW_PARSE_ARGS: { int i = 0; char** argv = (char**)argp; for (i = 0; i < argi; i++) { if (!strcmp(argv[i], "--mploop")) { loopclip = argv[++i]; break; } else if (!strcmp(argv[i], "--mpbin")) { mpbin = argv[++i]; } } } break; } return 0; }
int mpOpen(char* pchFilename, char* opts) { char buf[512]; mpClose(); snprintf(buf, sizeof(buf), "%s %s -slave -quiet %s", mpbin, pchFilename, opts ? opts : ""); printf("MPlayer command line:\n%s\n", buf); mpx.flags = SF_REDIRECT_STDIN | SF_REDIRECT_STDOUT; mpState = MP_LOADING; if (ShellExec(&mpx, buf)) return -1; return 0; }
int ehMpd(MW_EVENT event, void* arg) { switch (event) { case MW_INIT: memset(&mpx,0,sizeof(mpx)); break; case MW_UNINIT: mpClose(); break; } return 0; }
int mpOpen(char* pchFilename) { char cmd[256]; mpClose(); #ifdef WIN32 sprintf(cmd,"mplayer.exe -slave -quiet %s",pchFilename); #else sprintf(cmd,"/cygdrive/c/mplayer/mplayer -slave -quiet %s",pchFilename); #endif mpx.pchCommandLine=cmd; mpx.flags = SHELL_REDIRECT_STDIN; if (ShellExec(&mpx)) return -1; msleep(1000); mpState=1; return 0; }
//----------------------------------------------------------------------- // Start the tasks for a new state server connection: // - WaitForSimpleMsg: Task that waits to receive new SimpleMessage // - AddToIncQueueProcess: Task that take data from a message and generate Incmove //----------------------------------------------------------------------- void Ros_StateServer_StartNewConnection(Controller* controller, int sd) { int connectionIndex; printf("Starting new connection to the State Server\r\n"); //look for next available connection slot for (connectionIndex = 0; connectionIndex < MAX_STATE_CONNECTIONS; connectionIndex++) { if (controller->sdStateConnections[connectionIndex] == INVALID_SOCKET) { //Start the new connection in a different task. //Each task's memory will be unique IFF the data is on the stack. //Any global or heap stuff will not be unique. controller->sdStateConnections[connectionIndex] = sd; // If not started if(controller->tidStateSendState == INVALID_TASK) { //start task that will send the controller state controller->tidStateSendState = mpCreateTask(MP_PRI_TIME_NORMAL, MP_STACK_SIZE, (FUNCPTR)Ros_StateServer_SendState, (int)controller, 0, 0, 0, 0, 0, 0, 0, 0, 0); //set feedback signal if(controller->tidStateSendState != INVALID_TASK) Ros_Controller_SetIOState(IO_FEEDBACK_STATESERVERCONNECTED, TRUE); } break; } } if (connectionIndex == MAX_STATE_CONNECTIONS) { printf("Too many State server connections... not accepting last attempt.\r\n"); mpClose(sd); } }
int handleFileFunctionRequest(int acceptHandle, char *inBuffer, char *outBuffer, int type, int msgSize) { int32_t ret = -1; int32_t index = -1; int32_t mode = -1; int32_t flags = -1; int32_t fd = -1; int32_t maxBytes = -1; int sendRet = 0; int namelen = 0; short extensionId = -1; switch (type) { case FILE_CTRL_OPEN: flags = getInt32(inBuffer, 12); mode = getInt32(inBuffer, 16); ret = mpOpen(inBuffer + 20, flags, mode); setInt32(outBuffer, 0, 4); setInt32(outBuffer, 4, ret); sendRet = sendN(acceptHandle, outBuffer, 8, 0); if (sendRet != 8) { fprintf(stderr, "tcpSvr: sendRet = %d != 8\n", sendRet); return -1; } break; case FILE_CTRL_CREATE: flags = getInt32(inBuffer, 12); ret = mpCreate(inBuffer + 16, flags); setInt32(outBuffer, 0, 4); setInt32(outBuffer, 4, ret); sendRet = sendN(acceptHandle, outBuffer, 8, 0); if (sendRet != 8) { fprintf(stderr, "tcpSvr: sendRet = %d != 8\n", sendRet); return -1; } break; case FILE_CTRL_CLOSE: if (msgSize != 12) { fprintf(stderr, "tcpSvr: invalid msgSize for mpClose = %d != 12\n", msgSize); return -1; } fd = getInt32(inBuffer, 12); if (fd < 1) { fprintf(stderr, "tcpSvr: invalid fd for mpRead = %d\n", fd); return -1; } ret = mpClose(fd); setInt32(outBuffer, 0, 4); setInt32(outBuffer, 4, ret); sendRet = sendN(acceptHandle, outBuffer, 8, 0); if (sendRet != 8) { fprintf(stderr, "tcpSvr: sendRet = %d != 8\n", sendRet); return -1; } break; case FILE_CTRL_READ: fd = getInt32(inBuffer, 12); if (fd < 1) { fprintf(stderr, "tcpSvr: invalid fd for mpRead = %d\n", fd); return -1; } maxBytes = getInt32(inBuffer, 16); if (maxBytes < 1 || maxBytes >= (BUFF_MAX - 8)) { fprintf(stderr, "tcpSvr: invalid maxBytes for mpRead = %d max = %d\n", maxBytes,(BUFF_MAX-8)); return -1; } ret = mpRead(fd, outBuffer + 8, maxBytes); setInt32(outBuffer, 0, 4 + (ret > 0 ? ret : 0)); setInt32(outBuffer, 4, ret); sendRet = sendN(acceptHandle, outBuffer, 8 + (ret > 0 ? ret : 0), 0); if (sendRet != 8 + (ret > 0 ? ret : 0)) { fprintf(stderr, "tcpSvr: sendRet = %d != 8 + (ret > 0?ret:0)\n", sendRet); return -1; } break; case FILE_CTRL_WRITE: fd = getInt32(inBuffer, 12); if (fd < 1) { fprintf(stderr, "tcpSvr: invalid fd for mpRead = %d\n", fd); return -1; } maxBytes = getInt32(inBuffer, 16); if (maxBytes < 1 || maxBytes >= (BUFF_MAX - 8)) { fprintf(stderr, "tcpSvr: invalid maxBytes for mpRead = %d max = %d\n", maxBytes,(BUFF_MAX-8)); return -1; } ret = mpWrite(fd, inBuffer+20, maxBytes); setInt32(outBuffer, 0, 4 + (ret > 0 ? ret : 0)); setInt32(outBuffer, 4, ret); sendRet = sendN(acceptHandle, outBuffer, 8 + (ret > 0 ? ret : 0), 0); if (sendRet != 8 + (ret > 0 ? ret : 0)) { fprintf(stderr, "tcpSvr: sendRet = %d != 8 + (ret > 0?ret:0)\n", sendRet); return -1; } break; default: fprintf(stderr, "tcpSvr: invalid file function type = %d\n", type); return -1; } return 0; }
int uhMpd(UrlHandlerParam* param) { char *action; char *pbuf = param->pucBuffer; int bufsize = param->iDataBytes; HTTP_XML_NODE node; mwParseQueryString(param); action = mwGetVarValue(param->pxVars, "action", 0); mwWriteXmlHeader(&pbuf, &bufsize, 10, "gb2312", mwGetVarValue(param->pxVars, "xsl", 0)); mwWriteXmlString(&pbuf, &bufsize, 0, "<response>"); node.indent = 1; node.name = "state"; node.fmt = "%s"; node.flags = 0; if (!strcmp(param->pucRequest + 1, "playlist")) { PL_ENTRY *ptr; int i; if (!action) { } else if (!strcmp(action, "add")) { char *filename = mwGetVarValue(param->pxVars, "stream", 0); char *title = mwGetVarValue(param->pxVars, "title", 0); if (!title) title = ""; if (!filename) { node.value = "error"; } else { int fnlen; int titlelen; char *entrydata; mwDecodeString(filename); fnlen = strlen(filename); titlelen = strlen(title); entrydata = (char*)malloc(fnlen + titlelen + 3); strcpy(entrydata, filename); strcpy(entrydata + fnlen + 1, title); node.value = plAddEntry(&playlist, entrydata, fnlen + titlelen + 2) ? "OK" : "error"; } mwWriteXmlLine(&pbuf, &bufsize, &node, 0); } else if (!strcmp(action, "pin")) { int index = mwGetVarValueInt(param->pxVars, "arg", 0); node.value = plPinEntryByIndex(&playlist, index) ? "OK" : "error"; mwWriteXmlLine(&pbuf, &bufsize, &node, 0); } else if (!strcmp(action, "del")) { int index = mwGetVarValueInt(param->pxVars, "arg", 0); node.value = plDelEntryByIndex(&playlist, index) ? "OK" : "error"; mwWriteXmlLine(&pbuf, &bufsize, &node, 0); } ptr = playlist; mwWriteXmlString(&pbuf, &bufsize, 1, "<playlist>"); for (i=0; ptr; ptr = ptr->next, i++) { char buf[32]; snprintf(buf, sizeof(buf), "<item index=\"%03d\">", i); mwWriteXmlString(&pbuf, &bufsize, 2, buf); node.indent = 3; node.name = "stream"; node.fmt = "%s"; node.value = ptr->data; mwWriteXmlLine(&pbuf, &bufsize, &node, 0); node.flags = XN_CDATA; node.name = "title"; node.value = (char*)ptr->data + strlen(ptr->data) + 1; mwWriteXmlLine(&pbuf, &bufsize, &node, 0); mwWriteXmlString(&pbuf, &bufsize, 2, "</item>"); } mwWriteXmlString(&pbuf, &bufsize, 1, "</playlist>"); } else if (!action) { } else if (!strcmp(action, "play")) { char *filename = mwGetVarValue(param->pxVars, "stream", 0); char *args = mwGetVarValue(param->pxVars, "arg", 0); if (filename && *filename) { mwDecodeString(filename); filename = strdup(filename); } if ((!filename || !*filename) || plAddEntry(&playlist, filename, strlen(filename) + 1)) { if (!mpThreadHandle) { if (args) mwDecodeString(args); ThreadCreate(&mpThreadHandle, mpThread, args); } else { mpClose(); } node.value = "OK"; } else { node.value = "error"; } mwWriteXmlLine(&pbuf, &bufsize, &node, 0); } else if (!strcmp(action, "query")) { int i; char* info; for (i = 0; info = mwGetVarValue(param->pxVars, "info", i); i++) { if (!strcmp(info, "pos")) { node.name = "pos"; node.fmt = "%d"; node.value = (void*)mpPos; mwWriteXmlLine(&pbuf, &bufsize, &node, 0); } else if (!strcmp(info, "state")) { node.name = "state"; node.fmt = "%s"; node.value = states[mpState]; mwWriteXmlLine(&pbuf, &bufsize, &node, 0); } } } else if (!strcmp(action, "pause")) { mpState = (mpState == MP_PLAYING ? MP_PAUSED : MP_PLAYING); mpCommand("pause"); } else if (!strcmp(action, "seek")) { char buf[32]; char *args = mwGetVarValue(param->pxVars, "arg", 0); if (args) { snprintf(buf, sizeof(buf), "seek %s", args); mpCommand(buf); } } else if (!strcmp(action, "command")) { char *cmd = mwGetVarValue(param->pxVars, "arg", 0); char *hasResult = mwGetVarValue(param->pxVars, "result", 0); if (cmd) { if (mpCommand(cmd) > 0) { node.value = "OK"; if (hasResult) { int bytes; MutexLock(&mpConsoleMutex); bytes = snprintf(pbuf, bufsize, " <result><![CDATA["); pbuf += bytes; bufsize -= bytes; bytes = mpRead(pbuf, bufsize); MutexUnlock(&mpConsoleMutex); if (bytes > 0) { pbuf += bytes; bufsize -= bytes; } bytes = snprintf(pbuf, bufsize, "]]></result>"); pbuf += bytes; bufsize -= bytes; } } else { node.value = "error"; } mwWriteXmlLine(&pbuf, &bufsize, &node, 0); } } else { return 0; } mwWriteXmlString(&pbuf, &bufsize, 0, "</response>"); param->iDataBytes=(int)(pbuf-param->pucBuffer); param->fileType=HTTPFILETYPE_XML; return FLAG_DATA_RAW; }
// Called from the app framework. void appDeinit() { mpuCloseFace(aPhotoFace, texPhoto, imgPhoto); mpClose(); aPhotoFace = NULL; }
bool Controller::writeJob(char* path, char* job) { bool rtn = false; int fd = this->MP_ERROR; int status = this->MP_ERROR; char filename[FILE_NAM_BUFFER_SIZE]; //should be big enough to hold a file name and DRAM drive name memset(filename, '\0', FILE_NAM_BUFFER_SIZE); //not sure this is needed, strcpy below also does this. strcpy(filename, "MPRAM1:\\"); strcat(filename, path); LOG_DEBUG("writeJob: %s", filename); // Remove the file, if it exists LOG_DEBUG("Trying to remove file, if it exists"); status = mpRemove( filename ); if (this->MP_ERROR == status) { LOG_WARN("Failed to remove job file: %s", filename); } // Create the file and write the job fd = mpCreate( filename, O_WRONLY ); if (this->MP_ERROR != fd) { status = mpWrite( fd, job, strlen(job) ); if ( this->MP_ERROR != status ) { LOG_INFO("Successfully loaded file: %s, bytes written: %d", filename, status); rtn = true; } else { LOG_ERROR("Failed to write file: %s", filename); rtn = false; } // Checking file status /* struct stat pStat; status = mpFstat(fd, &pStat); { LOG_DEBUG("mpFstat Complete"); LOG_DEBUG("st_dev = %u", pStat.st_dev); LOG_DEBUG("st_ino = %u", pStat.st_ino); LOG_DEBUG("st_mode = %u", pStat.st_mode); LOG_DEBUG("st_nlink = %d", pStat.st_nlink); LOG_DEBUG("st_uid = %d", pStat.st_uid); LOG_DEBUG("st_gid = %d", pStat.st_gid); LOG_DEBUG("st_rdev = %u", pStat.st_rdev); LOG_DEBUG("st_size = %u", pStat.st_size); LOG_DEBUG("st_atime = %u", pStat.st_atime); LOG_DEBUG("st_mtime = %u", pStat.st_mtime); LOG_DEBUG("st_ctime = %u", pStat.st_ctime); LOG_DEBUG("st_blksize = %u", pStat.st_blksize); LOG_DEBUG("st_blocks = %u", pStat.st_blocks); LOG_DEBUG("st_attrib = %u", pStat.st_attrib); } */ // close file descriptor status = mpClose(fd); if (this->MP_ERROR == status) { LOG_WARN("Failed to close file: %s, ignoring failure", filename); } } else { LOG_ERROR("Failed to create job file: %s", filename); rtn = false; } return rtn; }