PageRenderTime 27ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/decrypt/cam.c

https://bitbucket.org/a84/gs_public/
C | 114 lines | 91 code | 20 blank | 3 comment | 19 complexity | 63a7737899632b9efb7bb344f2a4e434 MD5 | raw file
  1/*
  2 * Author: Andrey Dyldin <and@cesbo.com>
  3 */
  4
  5#include <stdio.h>
  6#include <unistd.h>
  7#include <string.h>
  8#include <signal.h>
  9#include <syscall.h>
 10
 11#include "newcamd.h"
 12#include "camd35.h"
 13#include "cam.h"
 14#include "../getstream.h"
 15#include "../config.h"
 16
 17int cam_login(cam_t *cam) {
 18	if(cam->loginOk == -1) return 0;
 19	cam->loginOk = -1;
 20	int ret = cam->login(cam);
 21	if(ret > 0) cam->loginOk = 1;
 22	else cam->loginOk = 0;
 23	return ret;
 24}
 25
 26void cam_logout(cam_t *cam) {
 27	cam->logout(cam);
 28	cam_packet_t tmp;
 29	while(fifo_pop(cam->packet_buffer, &tmp) > 0);
 30	cam->loginOk = 0;
 31}
 32
 33void *cam_thread(void *arg) {
 34	cam_t *cam = (cam_t *)arg;
 35#ifdef DEBUG
 36	pid_t tid = (pid_t) syscall (SYS_gettid);
 37	printf("[cam:%d] thread = %d\n", cam->id, tid);
 38#endif
 39	pthread_mutex_init(&cam->mutex, NULL);
 40
 41	cam_login(cam);
 42
 43	while(cam->main_loop) {
 44		if(cam->loginOk < 0) {
 45			usleep(500);
 46			continue;
 47		}
 48		else if(cam->loginOk == 0) {
 49			cam_logout(cam);
 50			sleep(2);
 51			cam_login(cam);
 52			continue;
 53		}
 54
 55		cam_packet_t packet;
 56		if(fifo_pop(cam->packet_buffer, &packet) != sizeof(cam_packet_t)) {
 57			usleep(500);
 58			continue;
 59		}
 60
 61		cam->send_packet(cam, &packet);
 62	}
 63
 64	pthread_exit(NULL);
 65}
 66
 67void cam_add_packet(cam_t *cam, cam_packet_t *packet) {
 68	if(!cam || !packet || !cam->sys || cam->loginOk <= 0) return;
 69
 70	pthread_mutex_lock(&cam->mutex);
 71	int fifo_ret = fifo_push(cam->packet_buffer, packet);
 72	pthread_mutex_unlock(&cam->mutex);
 73	if(fifo_ret != sizeof(cam_packet_t)) {
 74		struct config_s *config = (struct config_s *)cam->config;
 75		monitor_send_msg(config->monitor
 76				, "[cam:%d] buffer %s [drop packet pnr:%d]"
 77				, cam->id, (fifo_ret==-2)?"overflow":"collision", packet->prg_id);
 78    }
 79}
 80
 81int cam_init(cam_t *cam) {
 82	if(!cam) return 0;
 83
 84	int ret = 0;
 85	cam->packet_buffer = fifo_init(cam->fifo_size, sizeof(cam_packet_t));
 86	switch(cam->type) {
 87		case CAM_CAMD35: ret = camd35_init(cam); break;
 88		case CAM_NEWCAMD: ret = newcamd_init(cam); break;
 89		default: break;
 90	}
 91
 92	if(ret > 0) {
 93		cam->main_loop = 1;
 94		pthread_create(&cam->thread, NULL, cam_thread, cam);
 95		pthread_detach(cam->thread);
 96	}
 97
 98	return ret;
 99}
100
101void cam_destroy(cam_t *cam) {
102	if(!cam) return;
103
104	pthread_kill(cam->thread, SIGINT);
105	cam->logout(cam);
106
107	switch(cam->type) {
108		case CAM_CAMD35: camd35_destroy(cam); break;
109		case CAM_NEWCAMD: newcamd_destroy(cam); break;
110		default: break;
111	}
112
113	fifo_destroy(cam->packet_buffer);
114}