tizen beta release
[framework/location/gps-manager.git] / gps-manager / nmea_logger.c
1 /*
2  * gps-manager
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include "nmea_logger.h"
29 #include "debug_util.h"
30
31 #define MAX_NMEA_RAW_DATA_LOG_FILE_CNT  (999)
32 #define MAX_NMEA_LOG_FILE_PATH          (100)
33 #define NMEA_LOGGING_FILE_PATH          "/opt/data/gps-manager/res/nmea_data"
34
35 int raw_nmea_fd = -1;
36
37 static int open_nmea_log_file(const char *);
38
39 void start_nmea_log()
40 {
41         char filepath[MAX_NMEA_LOG_FILE_PATH];
42
43         if (open_nmea_log_file(filepath) == -1) {
44                 LOG_GPS(DBG_ERR, "Starting LBS Logging for RAW NMEA data FAILED!");
45                 raw_nmea_fd = -1;
46                 return;
47         }
48
49         raw_nmea_fd = open(filepath, O_RDWR | O_APPEND | O_CREAT, 0644);
50
51         if (raw_nmea_fd < -1) {
52                 LOG_GPS(DBG_ERR, "FAILED to open [%s], error[%d]", filepath, errno);
53         } else {
54                 LOG_GPS(DBG_LOW, "%s raw_nmea_fd [%d]", filepath, raw_nmea_fd);
55         }
56
57         return;
58 }
59
60 void stop_nmea_log()
61 {
62         int close_ret_val = 0;
63
64         LOG_GPS(DBG_LOW, "raw_nmea_fd [%d]", raw_nmea_fd);
65
66         if (raw_nmea_fd != -1) {
67                 close_ret_val = close(raw_nmea_fd);
68                 if (close_ret_val < 0) {
69                         LOG_GPS(DBG_ERR, "FAILED to close raw_nmea_fd[%d], error[%d]", raw_nmea_fd, errno);
70                 }
71                 raw_nmea_fd = -1;
72         }
73         return;
74 }
75
76 void write_nmea_log(char *data, int len)
77 {
78         int write_ret_val = 0;
79
80         if (raw_nmea_fd != -1) {
81                 write_ret_val = write(raw_nmea_fd, data, len);
82                 if (write_ret_val < 0) {
83                         LOG_GPS(DBG_ERR, "FAILED to write[%d], error[%d]", raw_nmea_fd, errno);
84                         close(raw_nmea_fd);
85                         raw_nmea_fd = -1;
86                 }
87         }
88         return;
89 }
90
91 static int open_nmea_log_file(const char *filepath)
92 {
93         int idx = 0;
94         int fd = 0;
95         char fn[MAX_NMEA_LOG_FILE_PATH];
96
97         for (idx = 0; idx < MAX_NMEA_RAW_DATA_LOG_FILE_CNT; idx++) {
98                 snprintf(fn, MAX_NMEA_LOG_FILE_PATH, "%s%03d.log", NMEA_LOGGING_FILE_PATH, idx);
99                 fd = open(fn, O_RDONLY);
100                 if (fd == -1) {
101                         LOG_GPS(DBG_LOW, "next log file [%s]", fn);
102                         return 0;
103                 } else {
104                         close(fd);
105                 }
106         }
107         LOG_GPS(DBG_LOW, "All NMEA RAW Data logging files are used. New log file can not be created");
108         return -1;
109 }