📄 upnp_tv_ctrlpt.c
字号:
} break; case UPNP_EVENT_AUTORENEWAL_FAILED: case UPNP_EVENT_SUBSCRIPTION_EXPIRED: { int TimeOut=default_timeout; Upnp_SID newSID; int ret; struct Upnp_Event_Subscribe *es_event = (struct Upnp_Event_Subscribe * ) Event; ret=UpnpSubscribe(ctrlpt_handle, es_event->PublisherUrl, &TimeOut, newSID); if (ret==UPNP_E_SUCCESS) { printf("Subscribed to EventURL with SID=%s\n", newSID); TvCtrlPointHandleSubscribeUpdate(es_event->PublisherUrl, newSID, TimeOut); } else { printf("Error Subscribing to EventURL -- %d\n", ret); } } break; /* ignore these cases, since this is not a device */ case UPNP_EVENT_SUBSCRIPTION_REQUEST: case UPNP_CONTROL_GET_VAR_REQUEST: case UPNP_CONTROL_ACTION_REQUEST: break; } return(0);}/******************************************************************************** * TvCtrlPointVerifyTimeouts * * Description: * Checks the advertisement and subscription timeouts for each device * and service in the global device list. If an advertisement expires, * the device is removed from the list. If an advertisement is about to * expire, a search request is sent for that device. If a subscription * expires, request a new one. If a subscription is about to expire, * try to renew it. * * Parameters: * incr -- The increment to subtract from the timeouts each time the * function is called. * ********************************************************************************/void TvCtrlPointVerifyTimeouts(int incr){ struct TvDeviceNode *prevdevnode, *curdevnode; int ret; pthread_mutex_lock(&DeviceListMutex); prevdevnode = curdevnode = GlobalDeviceList; while (curdevnode) { curdevnode->device.AdvrTimeOut -= incr; //printf("Advertisement Timeout: %d\n", curdevnode->device.AdvrTimeOut); if (curdevnode->device.AdvrTimeOut <= 0) { /* This advertisement has expired, so we should remove the device from the list */ if (GlobalDeviceList == curdevnode) GlobalDeviceList = curdevnode->next; prevdevnode->next = curdevnode->next; TvCtrlPointDeleteNode(curdevnode); } else { if (curdevnode->device.AdvrTimeOut < 2*incr) { /* This advertisement is about to expire, so send out a search request for this device UDN to try to renew */ ret = UpnpSearchAsync(ctrlpt_handle, incr, curdevnode->device.UDN, NULL); if (ret != UPNP_E_SUCCESS) printf("Error sending search request for Device UDN: %s -- err = %d\n", curdevnode->device.UDN, ret); } prevdevnode = curdevnode; } curdevnode = curdevnode->next; } pthread_mutex_unlock(&DeviceListMutex);}/******************************************************************************** * TvCtrlPointPrintCommands * * Description: * Print the list of valid command line commands to the user * * Parameters: * None * ********************************************************************************/void TvCtrlPointPrintCommands() { int i; int numofcmds=sizeof(cmdloop_cmdlist)/sizeof(cmdloop_commands); printf("\nValid Commands:\n"); for (i=0; i<numofcmds; i++) { printf(" %-14s %s\n", cmdloop_cmdlist[i].str, cmdloop_cmdlist[i].args); } printf("\n");}/******************************************************************************** * TvCtrlPointCommandLoop * * Description: * Function that receives commands from the user at the command prompt * during the lifetime of the control point, and calls the appropriate * functions for those commands. * * Parameters: * None * ********************************************************************************/void* TvCtrlPointCommandLoop(void *args){ int stoploop=0; char cmdline[100]; char cmd[100]; char strarg[100]; int arg1; int arg2; int cmdnum=-1; int numofcmds=sizeof(cmdloop_cmdlist)/sizeof(cmdloop_commands); int cmdfound=0; int i; int invalid_args; int arg_val_err=-99999; while (!stoploop) { cmdfound=0; sprintf(cmdline, " "); sprintf(cmd, " "); arg1 = arg_val_err; arg2 = arg_val_err; invalid_args=0; printf("\n>> "); // Get a command line fgets(cmdline, 100, stdin); /* Get the command name (first string of command line). Also, assume two integer arguments and parse those. */ sscanf(cmdline, "%s %d %d", cmd, &arg1, &arg2); for(i = 0; i < numofcmds; i++) { if (strcasecmp(cmd, cmdloop_cmdlist[i].str) == 0) { cmdnum = cmdloop_cmdlist[i].cmdnum; cmdfound = 1; break; } } if (cmdfound) { switch(cmdnum) { case PRTHELP: TvCtrlPointPrintHelp(); break; case POWON: if (arg1 == arg_val_err) invalid_args = 1; else TvCtrlPointSendAction(TV_SERVICE_CONTROL, arg1, "PowerOn", NULL, NULL, 0); break; case POWOFF: if (arg1 == arg_val_err) invalid_args = 1; else TvCtrlPointSendAction(TV_SERVICE_CONTROL, arg1, "PowerOff", NULL, NULL, 0); break; case SETCHAN: if (arg1 == arg_val_err || arg2 == arg_val_err) invalid_args = 1; else TvCtrlPointSendSetChannel(arg1, arg2); break; case SETVOL: if (arg1 == arg_val_err || arg2 == arg_val_err) invalid_args = 1; else TvCtrlPointSendSetVolume(arg1, arg2); break; case SETCOL: if (arg1 == arg_val_err || arg2 == arg_val_err) invalid_args = 1; else TvCtrlPointSendSetColor(arg1, arg2); break; case SETTINT: if (arg1 == arg_val_err || arg2 == arg_val_err) invalid_args = 1; else TvCtrlPointSendSetTint(arg1, arg2); break; case SETCONT: if (arg1 == arg_val_err || arg2 == arg_val_err) invalid_args = 1; else TvCtrlPointSendSetContrast(arg1, arg2); break; case SETBRT: if (arg1 == arg_val_err || arg2 == arg_val_err) invalid_args = 1; else TvCtrlPointSendSetBrightness(arg1, arg2); break; case CTRLACTION: /* re-parse commandline since second arg is string */ sscanf(cmdline, "%s %d %s", cmd, &arg1, strarg); if (arg1 == arg_val_err) invalid_args = 1; else TvCtrlPointSendAction(TV_SERVICE_CONTROL, arg1, strarg, NULL, NULL, 0); break; case PICTACTION: /* re-parse commandline since second arg is string */ sscanf(cmdline, "%s %d %s", cmd, &arg1, strarg); if (arg1 == arg_val_err) invalid_args = 1; else TvCtrlPointSendAction(TV_SERVICE_PICTURE, arg1, strarg, NULL, NULL, 0); break; case CTRLGETVAR: /* re-parse commandline since second arg is string */ sscanf(cmdline, "%s %d %s", cmd, &arg1, strarg); if (arg1 == arg_val_err) invalid_args = 1; else TvCtrlPointGetVar(TV_SERVICE_CONTROL, arg1, strarg); break; case PICTGETVAR: /* re-parse commandline since second arg is string */ sscanf(cmdline, "%s %d %s", cmd, &arg1, strarg); if (arg1 == arg_val_err) invalid_args = 1; else TvCtrlPointGetVar(TV_SERVICE_PICTURE, arg1, strarg); break; case PRTDEV: if (arg1 == arg_val_err) invalid_args = 1; else TvCtrlPointPrintDevice(arg1); break; case LSTDEV: TvCtrlPointPrintList(); break; case REFRESH: TvCtrlPointRefresh(); break; case EXITCMD: printf("Shutting down...\n"); UpnpUnRegisterClient(ctrlpt_handle); UpnpFinish(); exit(0); break; default: printf("command not yet implemented: %s\n", cmd); TvCtrlPointPrintCommands(); } if (invalid_args) { printf("Invalid args in command: %s\n", cmd); TvCtrlPointPrintCommands(); } } else { printf("Unknown command: %s\n", cmd); TvCtrlPointPrintCommands(); } } return(NULL);}/******************************************************************************** * TvCtrlPointTimerLoop * * Description: * Function that runs in its own thread and monitors advertisement * and subscription timeouts for devices in the global device list. * * Parameters: * None * ********************************************************************************/void* TvCtrlPointTimerLoop(void *args){ int incr = 30; // how often to verify the timeouts while (1) { sleep(incr); TvCtrlPointVerifyTimeouts(incr); } }int main(int argc, char** argv){ int ret=1; pthread_t timer_thread, cmdloop_thread; int code; int port; char *ip_address; int sig; sigset_t sigs_to_catch; if (argc!=3) { printf("wrong number of arguments\n Usage: %s ipaddress port\n", argv[0]); printf("\tipaddress: IP address of the device (must match desc. doc)\n"); printf("\t\te.g.: 192.168.0.2\n"); printf("\tport: Port number to use for receiving UPnP messages (must match desc. doc)\n"); printf("\t\te.g.: 5432\n"); exit(1); } ip_address=argv[1]; sscanf(argv[2],"%d",&port); printf("Intializing UPnP \n\twith ipaddress=%s port=%d \n", ip_address, port); if ((ret = UpnpInit(ip_address, port))) { printf("Error with UpnpInit -- %d\n", ret); UpnpFinish(); exit(1); } printf("UPnP Initialized\n"); printf("Registering the Control Point\n"); if ((ret = UpnpRegisterClient(TvCtrlPointCallbackEventHandler, &ctrlpt_handle, &ctrlpt_handle))) { printf("Error registering control point : %d\n", ret); UpnpFinish(); exit(1); } printf("Control Point Registered\n"); TvCtrlPointRefresh(); // start a command loop thread code = pthread_create( &cmdloop_thread, NULL, TvCtrlPointCommandLoop, NULL ); // start a timer thread code = pthread_create( &timer_thread, NULL, TvCtrlPointTimerLoop, NULL ); /* Catch Ctrl-C and properly shutdown */ sigemptyset(&sigs_to_catch); sigaddset(&sigs_to_catch, SIGINT); sigwait(&sigs_to_catch, &sig); printf("Shutting down on signal %d...\n", sig); UpnpUnRegisterClient(ctrlpt_handle); UpnpFinish(); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -