1 
2 /*
3 linphone
4 Copyright (C) 2010  Belledonne Communications SARL
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20 
21 /**
22  * @defgroup chatroom_tuto Chat room and messaging
23  * @ingroup tutorials
24  *This program is a _very_ simple usage example of liblinphone,
25  *desmonstrating how to send/receive  SIP MESSAGE from a sip uri identity passed from the command line.
26  *<br>Argument must be like sip:jehan@sip.linphone.org .
27  *<br>
28  *ex chatroom sip:jehan@sip.linphone.org
29  *<br>
30  *@include chatroom.c
31 
32  *
33  */
34 
35 #include "linphone/core.h"
36 
37 #include <signal.h>
38 
39 static bool_t running=TRUE;
40 
stop(int signum)41 static void stop(int signum){
42 	running=FALSE;
43 }
44 /**
45  * function invoked to report file transfer progress.
46  * */
file_transfer_progress_indication(LinphoneChatMessage * message,const LinphoneContent * content,size_t offset,size_t total)47 static void file_transfer_progress_indication(LinphoneChatMessage *message, const LinphoneContent* content, size_t offset, size_t total) {
48 	const LinphoneAddress* from_address = linphone_chat_message_get_from(message);
49 	const LinphoneAddress* to_address = linphone_chat_message_get_to(message);
50 	char *address = linphone_chat_message_is_outgoing(message)?linphone_address_as_string(to_address):linphone_address_as_string(from_address);
51 	printf(" File transfer  [%d%%] %s of type [%s/%s] %s [%s] \n", (int)((offset *100)/total)
52 																	,(linphone_chat_message_is_outgoing(message)?"sent":"received")
53 																	, linphone_content_get_type(content)
54 																	, linphone_content_get_subtype(content)
55 																	,(linphone_chat_message_is_outgoing(message)?"to":"from")
56 																	, address);
57 	free(address);
58 }
59 /**
60  * function invoked when a file transfer is received.
61  **/
file_transfer_received(LinphoneChatMessage * message,const LinphoneContent * content,const LinphoneBuffer * buffer)62 static void file_transfer_received(LinphoneChatMessage *message, const LinphoneContent* content, const LinphoneBuffer *buffer){
63 	FILE* file=NULL;
64 	if (!linphone_chat_message_get_user_data(message)) {
65 		/*first chunk, creating file*/
66 		file = fopen("receive_file.dump","wb");
67 		linphone_chat_message_set_user_data(message,(void*)file); /*store fd for next chunks*/
68 	}
69 
70 	file = (FILE*)linphone_chat_message_get_user_data(message);
71 	if (linphone_buffer_is_empty(buffer)) {
72 		printf("File transfert completed\n");
73 		linphone_chat_message_destroy(message);
74 		fclose(file);
75 		running=FALSE;
76 	} else { /* store content on a file*/
77 		if (fwrite(linphone_buffer_get_content(buffer),linphone_buffer_get_size(buffer),1,file)==0){
78 			ms_warning("file_transfer_received() write failed: %s",strerror(errno));
79 		}
80 	}
81 }
82 
83 char big_file [128000];
84 
85 /*
86  * function called when the file transfer is initiated. file content should be feed into object LinphoneContent
87  * */
file_transfer_send(LinphoneChatMessage * message,const LinphoneContent * content,size_t offset,size_t size)88 static LinphoneBuffer * file_transfer_send(LinphoneChatMessage *message, const LinphoneContent* content, size_t offset, size_t size){
89 	size_t size_to_send = MIN(size, sizeof(big_file) - offset);
90 	if (size == 0) return linphone_buffer_new(); /*end of file*/
91 	return linphone_buffer_new_from_data((uint8_t *)big_file + offset, size_to_send);
92 }
93 
94 /*
95  * Call back to get delivery status of a message
96  * */
linphone_file_transfer_state_changed(LinphoneChatMessage * msg,LinphoneChatMessageState state)97 static void linphone_file_transfer_state_changed(LinphoneChatMessage* msg,LinphoneChatMessageState state) {
98 	const LinphoneAddress* to_address = linphone_chat_message_get_to(msg);
99 	char *to = linphone_address_as_string(to_address);
100 	printf("File transfer sent to [%s] delivery status is [%s] \n"	, to
101 																	, linphone_chat_message_state_to_string(state));
102 	free(to);
103 }
104 
105 /*
106  * Call back called when a message is received
107  */
message_received(LinphoneCore * lc,LinphoneChatRoom * cr,LinphoneChatMessage * msg)108 static void message_received(LinphoneCore *lc, LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
109 	const LinphoneContent *file_transfer_info = linphone_chat_message_get_file_transfer_information(msg);
110 	printf ("Do you really want to download %s (size %ld)?[Y/n]\nOk, let's go\n", linphone_content_get_name(file_transfer_info), (long int)linphone_content_get_size(file_transfer_info));
111 
112 	linphone_chat_message_download_file(msg);
113 
114 }
115 
116 LinphoneCore *lc;
main(int argc,char * argv[])117 int main(int argc, char *argv[]){
118 	LinphoneCoreVTable vtable={0};
119 
120 	const char* dest_friend=NULL;
121 	size_t i;
122 	const char* big_file_content="big file";
123 	LinphoneChatRoom* chat_room;
124 	LinphoneContent* content;
125 	LinphoneChatMessage* chat_message;
126 	LinphoneChatMessageCbs *cbs;
127 
128 	/*seting dummy file content to something*/
129 	for (i=0;i<sizeof(big_file);i+=strlen(big_file_content))
130 		memcpy(big_file+i, big_file_content, strlen(big_file_content));
131 
132 	big_file[0]=*"S";
133 	big_file[sizeof(big_file)-1]=*"E";
134 
135 	signal(SIGINT,stop);
136 //#define DEBUG
137 #ifdef DEBUG
138 	linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
139 #endif
140 	vtable.message_received=message_received;
141 
142 
143 	/*
144 	 Instantiate a LinphoneCore object given the LinphoneCoreVTable
145 	*/
146 	lc=linphone_core_new(&vtable,NULL,NULL,NULL);
147 	dest_friend = linphone_core_get_primary_contact(lc);
148 	printf("Send message to me : %s\n", dest_friend);
149 
150 	/**
151 	 * Globally configure an http file transfer server.
152 	 */
153 	//linphone_core_set_file_transfer_server(lc,"http://npasc.al/lft.php");
154 	linphone_core_set_file_transfer_server(lc,"https://www.linphone.org:444/lft.php");
155 
156 
157 	/*Next step is to create a chat room*/
158 	chat_room = linphone_core_get_chat_room_from_uri(lc,dest_friend);
159 
160 	content = linphone_core_create_content(lc);
161 	linphone_content_set_type(content,"text");
162 	linphone_content_set_subtype(content,"plain");
163 	linphone_content_set_size(content,sizeof(big_file)); /*total size to be transfered*/
164 	linphone_content_set_name(content,"bigfile.txt");
165 
166 	/*now create a chat message with custom content*/
167 	chat_message = linphone_chat_room_create_file_transfer_message(chat_room,content);
168 	if (chat_message == NULL) {
169 		printf("returned message is null\n");
170 	}
171 
172 	/**
173 	 * Fill the application callbacks. The file_transfer_received callback is used in order to get notifications
174 	 * about incoming file reception, file_transfer_send to feed file to be transfered and
175 	 * file_transfer_progress_indication to print progress.
176 	 */
177 	cbs = linphone_chat_message_get_callbacks(chat_message);
178 	linphone_chat_message_cbs_set_file_transfer_recv(cbs, file_transfer_received);
179 	linphone_chat_message_cbs_set_file_transfer_send(cbs, file_transfer_send);
180 	linphone_chat_message_cbs_set_file_transfer_progress_indication(cbs, file_transfer_progress_indication);
181 	linphone_chat_message_cbs_set_msg_state_changed(cbs, linphone_file_transfer_state_changed);
182 
183 	/*initiating file transfer*/
184 	linphone_chat_room_send_chat_message(chat_room, chat_message);
185 
186 	/* main loop for receiving incoming messages and doing background linphone core work: */
187 	while(running){
188 		linphone_core_iterate(lc);
189 		ms_usleep(50000);
190 	}
191 
192 
193 	printf("Shutting down...\n");
194 	linphone_content_unref(content);
195 	linphone_core_destroy(lc);
196 	printf("Exited\n");
197 	return 0;
198 }
199 
200