/camera/camlib

To get this branch, use:
bzr branch http://darksoft.org/webbzr/camera/camlib

« back to all changes in this revision

Viewing changes to pfcontrol/fgserver.c

  • Committer: Suren A. Chilingaryan
  • Date: 2011-02-13 01:33:37 UTC
  • Revision ID: csa@dside.dyndns.org-20110213013337-ibm4w4n5a3hu4k7u
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
#include <sys/stat.h>
 
5
#include <sys/types.h>
 
6
#include <sys/socket.h>
 
7
#include <sys/un.h>
 
8
#include <signal.h>
 
9
 
 
10
#include "pfcam.h"
 
11
 
 
12
#define DLLNAME "libFullAreaGray8.so"
 
13
#define read_socket "/tmp/pfserver.socket"
 
14
 
 
15
 
 
16
int port = 0;
 
17
 
 
18
int handleError(error)
 
19
{
 
20
        const char *s;
 
21
 
 
22
        s = pfGetErrorString(error);
 
23
        if (error < 0) {
 
24
                printf("Error: %s\n", s);
 
25
        } else {
 
26
                printf("Warning: %s\n", s);
 
27
        }
 
28
        return error;
 
29
}
 
30
 
 
31
 
 
32
int request(char *data, const char *prop, const char *val) {
 
33
    int error, warn;
 
34
    char name[50], manu[50];
 
35
    int mBytes, nBytes, version, type;
 
36
    TOKEN t;
 
37
 
 
38
 
 
39
    //show info about port to open
 
40
    pfPortInfo(port, manu, &mBytes, name, &nBytes, &version, &type);
 
41
 
 
42
    //open port:
 
43
    error = pfDeviceOpen(port);
 
44
    if (error < 0) {
 
45
        handleError(error);
 
46
        return -1;
 
47
    }
 
48
    else {
 
49
        warn = error;
 
50
    }
 
51
 
 
52
 
 
53
    //Check if higher baud rate is supported
 
54
    error = pfIsBaudRateSupported(port, 115200);
 
55
    if (error == 1) {
 
56
        error = pfSetBaudRate(port, 115200);
 
57
        if (error < 0) {
 
58
            handleError(error);
 
59
            pfDeviceClose(port);
 
60
            return -1;
 
61
        }
 
62
    } else {
 
63
        error = pfIsBaudRateSupported(port, 57600);
 
64
        if (error == 1){
 
65
            error = pfSetBaudRate(port, 57600);
 
66
            if (error < 0){
 
67
                handleError(error);
 
68
                pfDeviceClose(port);
 
69
                return -1;
 
70
            }
 
71
        }
 
72
    }
 
73
 
 
74
    t = pfProperty_ParseName(port, prop);
 
75
    if (t == INVALID_TOKEN) {
 
76
        printf("Property '%s' not found or bad index\n", prop);
 
77
        pfDeviceClose(port);
 
78
        return -1;
 
79
    }
 
80
 
 
81
    if (val) {
 
82
        error = pfDevice_SetProperty_String(port, t, val);
 
83
        if (error >= 0) {
 
84
            error = pfDevice_GetProperty_String(port, t, data, 64);
 
85
        }
 
86
    } else {
 
87
        error = pfDevice_GetProperty_String(port, t, data, 64);
 
88
    }
 
89
 
 
90
    if (error < 0) {
 
91
        handleError(error);
 
92
        pfDeviceClose(port);
 
93
        return -1;
 
94
    } else {
 
95
        warn = error;
 
96
    }
 
97
 
 
98
 
 
99
    if (warn){
 
100
        handleError(warn);
 
101
    }
 
102
    pfDeviceClose(port);
 
103
    return 0;
 
104
}
 
105
 
 
106
 
 
107
int main(int argc, char * argv[]) {
 
108
    int err;
 
109
    char *tmp;
 
110
    char data[64];
 
111
    int arg;
 
112
 
 
113
    int numOfPorts;
 
114
 
 
115
    signal (SIGPIPE, SIG_IGN);
 
116
 
 
117
    pfPortInit(&numOfPorts);
 
118
 
 
119
    if (argc > 1){
 
120
        arg = strtoul(argv[1], &tmp, 10);
 
121
        if (!*tmp){
 
122
            port = arg;
 
123
            if (port >= numOfPorts){
 
124
                port = 0;
 
125
            }
 
126
        }
 
127
    }
 
128
 
 
129
    unlink(read_socket);
 
130
 
 
131
    // Open sockets for reading and writing?!
 
132
    //
 
133
    int sockRead = socket (PF_UNIX, SOCK_STREAM, 0);
 
134
    int on = 1;
 
135
 
 
136
    if ( setsockopt( sockRead, SOL_SOCKET, SO_REUSEADDR,
 
137
                     (const char *) &on, sizeof(on)   ) < 0)
 
138
        perror("setsockopt(...,SO_REUSEADDR,...)");
 
139
 
 
140
    // Give the socket a name.
 
141
    struct sockaddr_un name;
 
142
    memset(&name, 0, sizeof(name));
 
143
    name.sun_family = AF_UNIX;
 
144
    strncpy(name.sun_path,read_socket,sizeof(name.sun_path));
 
145
    name.sun_path[sizeof(name.sun_path)-1]=0;
 
146
 
 
147
    if (bind (sockRead, (struct sockaddr *) &name, sizeof (name)) < 0)
 
148
        perror ("bind");
 
149
 
 
150
    // Listen to the ports
 
151
    if (listen (sockRead, 10) < 0)
 
152
        perror ("listen");
 
153
 
 
154
    chmod(read_socket, 0666);
 
155
 
 
156
    // Add to select set?!
 
157
    fd_set active_fd_set;
 
158
    fd_set select_fd_set;
 
159
 
 
160
    FD_ZERO (&active_fd_set);
 
161
    FD_SET (sockRead, &active_fd_set);
 
162
 
 
163
    // Server loop
 
164
    socklen_t size;
 
165
    struct sockaddr_un clientname;
 
166
    int sockNew;
 
167
    char msg[2048];
 
168
    int status;
 
169
 
 
170
    while (1) {
 
171
        select_fd_set = active_fd_set;
 
172
 
 
173
        status = select (FD_SETSIZE, &select_fd_set, NULL, NULL, NULL);
 
174
        if ((status > 0)&&(FD_ISSET(sockRead, &select_fd_set))) {
 
175
 
 
176
            //printf("New client connected for reading from camera\n");
 
177
            size = sizeof (clientname);
 
178
            if ((sockNew = accept (sockRead,(struct sockaddr *) &clientname, &size)) < 0){
 
179
                perror ("accept");
 
180
            }
 
181
 
 
182
            //  Transmit the requested number of bytes from the calling side !!!
 
183
            int res;
 
184
            res = read(sockNew, msg, 2048);
 
185
            if (res>0) {
 
186
                msg[res] = 0; // Terminate string
 
187
 
 
188
                tmp = strchr(msg, ' ');
 
189
                if (tmp) *(tmp++) = 0;
 
190
 
 
191
                err = request(data, msg, tmp);
 
192
                    // retrying in case of error
 
193
                if (err) err = request(data, msg, tmp);
 
194
                
 
195
                if (err) strcpy(data, "-1");
 
196
            } else {
 
197
                strcpy(data, "-1");
 
198
            }
 
199
 
 
200
            write(sockNew, data, strlen(data));
 
201
 
 
202
            close(sockNew);
 
203
        } // end of status > 0
 
204
    }
 
205
 
 
206
    return(0);
 
207
}